mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Input - Fix keys stuck on Ctrl+Shift+Esc or Ctrl+Alt+Del - closes #509
This commit is contained in:
parent
7dc157fa34
commit
9ba8f87c81
@ -47,7 +47,7 @@ namespace Artemis.Core.Services
|
||||
inputProvider.MouseScrollDataReceived += InputProviderOnMouseScrollDataReceived;
|
||||
inputProvider.MouseMoveDataReceived += InputProviderOnMouseMoveDataReceived;
|
||||
_inputProviders.Add(inputProvider);
|
||||
|
||||
|
||||
inputProvider.OnKeyboardToggleStatusRequested();
|
||||
}
|
||||
|
||||
@ -296,6 +296,17 @@ namespace Artemis.Core.Services
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
public void ReleaseAll()
|
||||
{
|
||||
foreach (var (device, keys) in _pressedKeys.ToList())
|
||||
{
|
||||
foreach (KeyboardKey keyboardKey in keys)
|
||||
{
|
||||
InputProviderOnKeyboardDataReceived(this, new InputProviderKeyboardEventArgs(device, keyboardKey, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mouse
|
||||
|
||||
@ -35,6 +35,11 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
void StopIdentify();
|
||||
|
||||
/// <summary>
|
||||
/// Flush all currently pressed buttons/keys
|
||||
/// </summary>
|
||||
void ReleaseAll();
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Timers;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Interop;
|
||||
using Artemis.Core;
|
||||
using Artemis.Core.Services;
|
||||
using Artemis.UI.Utilities;
|
||||
using Linearstar.Windows.RawInput;
|
||||
using Linearstar.Windows.RawInput.Native;
|
||||
using Serilog;
|
||||
@ -14,11 +19,12 @@ namespace Artemis.UI.InputProviders
|
||||
public class NativeWindowInputProvider : InputProvider
|
||||
{
|
||||
private const int WM_INPUT = 0x00FF;
|
||||
|
||||
private readonly IInputService _inputService;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
private DateTime _lastMouseUpdate;
|
||||
private SpongeWindow _sponge;
|
||||
private System.Timers.Timer _taskManagerTimer;
|
||||
|
||||
public NativeWindowInputProvider(ILogger logger, IInputService inputService)
|
||||
{
|
||||
@ -28,10 +34,15 @@ namespace Artemis.UI.InputProviders
|
||||
_sponge = new SpongeWindow();
|
||||
_sponge.WndProcCalled += SpongeOnWndProcCalled;
|
||||
|
||||
_taskManagerTimer = new System.Timers.Timer(500);
|
||||
_taskManagerTimer.Elapsed += TaskManagerTimerOnElapsed;
|
||||
_taskManagerTimer.Start();
|
||||
|
||||
RawInputDevice.RegisterDevice(HidUsageAndPage.Keyboard, RawInputDeviceFlags.InputSink, _sponge.Handle);
|
||||
RawInputDevice.RegisterDevice(HidUsageAndPage.Mouse, RawInputDeviceFlags.InputSink, _sponge.Handle);
|
||||
}
|
||||
|
||||
|
||||
#region Overrides of InputProvider
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -51,6 +62,8 @@ namespace Artemis.UI.InputProviders
|
||||
{
|
||||
_sponge?.DestroyHandle();
|
||||
_sponge = null;
|
||||
_taskManagerTimer?.Dispose();
|
||||
_taskManagerTimer = null;
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
@ -75,6 +88,15 @@ namespace Artemis.UI.InputProviders
|
||||
}
|
||||
}
|
||||
|
||||
private void TaskManagerTimerOnElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
// If task manager has focus then we can't track keys properly, release everything to avoid them getting stuck
|
||||
// Same goes for Idle which is what you get when you press Ctrl+Alt+Del
|
||||
Process active = Process.GetProcessById(WindowUtilities.GetActiveProcessId());
|
||||
if (active?.ProcessName == "Taskmgr" || active?.ProcessName == "Idle")
|
||||
_inputService.ReleaseAll();
|
||||
}
|
||||
|
||||
#region Keyboard
|
||||
|
||||
private void HandleKeyboardData(RawInputData data, RawInputKeyboardData keyboardData)
|
||||
|
||||
24
src/Artemis.UI/Utilities/WindowUtilities.cs
Normal file
24
src/Artemis.UI/Utilities/WindowUtilities.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Artemis.UI.Utilities
|
||||
{
|
||||
public static class WindowUtilities
|
||||
{
|
||||
public static int GetActiveProcessId()
|
||||
{
|
||||
// Get foreground window handle
|
||||
IntPtr hWnd = GetForegroundWindow();
|
||||
|
||||
GetWindowThreadProcessId(hWnd, out uint processId);
|
||||
return (int) processId;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user