Skip to content

Commit 1b03de8

Browse files
author
LegendaryB
authored
Merge pull request #26 from LegendaryB/feature/25-global-mouse-keyboard-hook-instead-of-getlastinputinfo
Merghe feature/25 global mouse keyboard hook instead of getlastinputinfo into develop
2 parents e3579dc + 94dc7aa commit 1b03de8

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/Kirei.Infrastructure/Kirei.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
1717
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
1818
<PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
19+
<PackageReference Include="NeatInput" Version="1.0.0" />
1920
</ItemGroup>
2021

2122
<ItemGroup>

src/Kirei.Infrastructure/Native/User32.Wrappers.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ namespace Kirei.Infrastructure.Native
99
{
1010
internal static partial class User32
1111
{
12-
internal struct LASTINPUTINFO
13-
{
14-
public uint cbSize;
15-
public uint dwTime;
16-
}
17-
18-
[DllImport("user32.dll")]
19-
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
20-
2112
[DllImport("user32.dll", CharSet = CharSet.Auto)]
2213
private static extern int GetClassName(
2314
IntPtr hWnd,
@@ -39,16 +30,6 @@ SetWindowPosFlags uFlags
3930
private static extern bool GetWindowRect(
4031
IntPtr hwnd,
4132
ref RECT rectangle);
42-
43-
internal static long GetUserIdleTime()
44-
{
45-
var lastInputStruct = new LASTINPUTINFO();
46-
lastInputStruct.cbSize = (uint)Marshal.SizeOf(lastInputStruct);
47-
48-
GetLastInputInfo(ref lastInputStruct);
49-
50-
return ((uint)Environment.TickCount - lastInputStruct.dwTime);
51-
}
5233

5334
internal static string GetClassName(IntPtr hWnd)
5435
{
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
using Kirei.Application.System.Input;
22
using Kirei.Infrastructure.Configuration;
3-
using Kirei.Infrastructure.Native;
43

4+
using NeatInput;
5+
6+
using System;
57
using System.Threading;
68

79
namespace Kirei.Infrastructure.System.Input
810
{
911
public class InputListener :
1012
IInputListener
1113
{
14+
private InputProvider _inputProvider;
1215
private IInputActionMapper _inputActionMapper;
13-
private bool hasIconsBeenHidden = false;
16+
17+
private DateTime lastInputReceivedAt;
18+
private bool hasIconsBeenHidden = false;
1419

1520
public void Listen(IInputActionMapper inputActionMapper)
1621
{
22+
_inputProvider = new InputProvider();
1723
_inputActionMapper = inputActionMapper;
1824

25+
lastInputReceivedAt = DateTime.Now;
26+
27+
_inputProvider.InputReceived += OnInputReceived;
28+
29+
var inactiveAfterMs = ConfigurationProvider.Configuration.Application.InactiveAfterMs;
30+
var inputPollingRateMs = ConfigurationProvider.Configuration.Application.InputPollingRate;
31+
1932
while (true)
2033
{
21-
var lastInputInMilliseconds = User32.GetUserIdleTime();
22-
var inactiveAfterMs = ConfigurationProvider.Configuration.Application.InactiveAfterMs;
34+
var lastInputInMilliseconds = DateTime.Now.Subtract(lastInputReceivedAt).TotalMilliseconds;
2335

2436
if (lastInputInMilliseconds >= inactiveAfterMs && !hasIconsBeenHidden)
2537
{
@@ -32,8 +44,13 @@ public void Listen(IInputActionMapper inputActionMapper)
3244
hasIconsBeenHidden = false;
3345
}
3446

35-
Thread.Sleep(ConfigurationProvider.Configuration.Application.InputPollingRate);
47+
Thread.Sleep(inputPollingRateMs);
3648
}
3749
}
50+
51+
private void OnInputReceived(NeatInput.Domain.Hooking.Input input)
52+
{
53+
lastInputReceivedAt = DateTime.Now;
54+
}
3855
}
3956
}

0 commit comments

Comments
 (0)