-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardHook.cs
More file actions
168 lines (151 loc) · 7.12 KB
/
KeyboardHook.cs
File metadata and controls
168 lines (151 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
namespace bws
{
public enum MoveDirection
{
Left, Right, Up, Down, Home, End, PageUp, PageDown
}
public class KeyboardHook : IDisposable
{
private Win32Interop.LowLevelKeyboardProc _proc;
private IntPtr _hookID = IntPtr.Zero;
public event EventHandler<bool>? AltTabOpen; // bool isSticky
public event EventHandler? AltReleased;
public event EventHandler? EnterPressed;
public event EventHandler? EscPressed;
public event EventHandler? QPressed;
public event EventHandler<MoveDirection>? DirectionKeyPressed;
public Func<bool>? IsSwitcherActive { get; set; }
public KeyboardHook()
{
_proc = HookCallback;
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule!)
{
_hookID = Win32Interop.SetWindowsHookEx(Win32Interop.WH_KEYBOARD_LL, _proc,
Win32Interop.GetModuleHandle(curModule.ModuleName!), 0);
}
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var hookStruct = Marshal.PtrToStructure<Win32Interop.KBDLLHOOKSTRUCT>(lParam);
int vkCode = (int)hookStruct.vkCode;
if (wParam == (IntPtr)Win32Interop.WM_SYSKEYDOWN || wParam == (IntPtr)Win32Interop.WM_KEYDOWN)
{
bool isAltPressed = (Win32Interop.GetAsyncKeyState(Win32Interop.VK_LMENU) & 0x8000) != 0 ||
(Win32Interop.GetAsyncKeyState(Win32Interop.VK_RMENU) & 0x8000) != 0;
bool isShiftPressed = (Win32Interop.GetAsyncKeyState(Win32Interop.VK_LSHIFT) & 0x8000) != 0 ||
(Win32Interop.GetAsyncKeyState(Win32Interop.VK_RSHIFT) & 0x8000) != 0;
bool isCtrlPressed = (Win32Interop.GetAsyncKeyState(Win32Interop.VK_LCONTROL) & 0x8000) != 0 ||
(Win32Interop.GetAsyncKeyState(Win32Interop.VK_RCONTROL) & 0x8000) != 0;
bool isActive = IsSwitcherActive != null && IsSwitcherActive();
if (!isActive)
{
if (vkCode == Win32Interop.VK_TAB && isAltPressed && !isShiftPressed)
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
AltTabOpen?.Invoke(this, isCtrlPressed);
});
return (IntPtr)1;
}
}
else
{
MoveDirection? dir = null;
if (vkCode == Win32Interop.VK_TAB)
{
dir = MoveDirection.Right;
}
else if (vkCode == 0x10 || vkCode == Win32Interop.VK_LSHIFT || vkCode == Win32Interop.VK_RSHIFT) // Shift, LShift, RShift
{
dir = MoveDirection.Left;
}
else if (vkCode == 0x44 || vkCode == 0x4C || vkCode == Win32Interop.VK_RIGHT) // D, L or Right
{
dir = MoveDirection.Right;
}
else if (vkCode == 0x41 || vkCode == 0x48 || vkCode == Win32Interop.VK_LEFT) // A, H or Left
{
dir = MoveDirection.Left;
}
else if ((vkCode == 0x53 || vkCode == 0x4A) && !isShiftPressed || vkCode == Win32Interop.VK_DOWN) // S, J or Down
{
dir = MoveDirection.Down;
}
else if (vkCode == 0x57 || vkCode == 0x4B || vkCode == Win32Interop.VK_UP) // W, K or Up
{
dir = MoveDirection.Up;
}
else if (vkCode == 0x24) // Home
{
dir = MoveDirection.Home;
}
else if (vkCode == 0x23) // End
{
dir = MoveDirection.End;
}
else if (vkCode == 0x21) // Page Up
{
dir = MoveDirection.PageUp;
}
else if (vkCode == 0x22) // Page Down
{
dir = MoveDirection.PageDown;
}
if (dir != null)
{
System.Windows.Application.Current.Dispatcher.Invoke(() => DirectionKeyPressed?.Invoke(this, dir.Value));
return (IntPtr)1;
}
if (vkCode == 0x0D || vkCode == 0x20) // Enter or Space
{
System.Windows.Application.Current.Dispatcher.Invoke(() => EnterPressed?.Invoke(this, EventArgs.Empty));
return (IntPtr)1;
}
if (vkCode == Win32Interop.VK_ESCAPE)
{
System.Windows.Application.Current.Dispatcher.Invoke(() => EscPressed?.Invoke(this, EventArgs.Empty));
return (IntPtr)1;
}
if (vkCode == 0x51) // Q
{
System.Windows.Application.Current.Dispatcher.Invoke(() => QPressed?.Invoke(this, EventArgs.Empty));
return (IntPtr)1;
}
// Block other typing keys while switcher is active to prevent typing into background apps
if (vkCode >= 0x41 && vkCode <= 0x5A) // A-Z
{
return (IntPtr)1;
}
}
}
else if (wParam == (IntPtr)Win32Interop.WM_KEYUP || wParam == (IntPtr)Win32Interop.WM_SYSKEYUP)
{
if (vkCode == Win32Interop.VK_LMENU || vkCode == Win32Interop.VK_RMENU)
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
AltReleased?.Invoke(this, EventArgs.Empty);
});
}
}
}
return Win32Interop.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
public void Dispose()
{
if (_hookID != IntPtr.Zero)
{
Win32Interop.UnhookWindowsHookEx(_hookID);
_hookID = IntPtr.Zero;
}
}
}
}