-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyboardListener.cs
224 lines (180 loc) · 6.36 KB
/
KeyboardListener.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NormalKeyboardSwitcher
{
delegate IntPtr KeyboardHookDelegate(int nCode, IntPtr wParam, IntPtr lParam);
delegate int NextTemporaryInputLanguageDelegate();
delegate void DropTemporaryInputLanguageDelegate();
delegate void SwitchToTemporaryInputLanguageDelegate();
[StructLayout(LayoutKind.Sequential)]
public class KBDLLHOOKSTRUCT
{
public Keys keys;
public int scanCode;
public int flags;
public uint time;
public IntPtr dwExtraInfo;
}
public enum FirstKey
{
Control,
LeftAlt
}
class KeyboardListener
{
private IntPtr keyboardHookID = IntPtr.Zero;
private bool firstPressed = false;
private bool secondPressed = false;
private bool enabled = false;
private FirstKey firstKey = FirstKey.Control;
private KeyboardHookDelegate KeyboardHookInstance;
public KeyboardListener(FirstKey firstKey) {
FirstKey = firstKey;
KeyboardHookInstance = new KeyboardHookDelegate(KeyboardHook);
Install();
}
~KeyboardListener()
{
Uninstall();
}
void Install()
{
keyboardHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookInstance, IntPtr.Zero, 0);
}
void Uninstall()
{
UnhookWindowsHookEx(keyboardHookID);
}
FirstKey FirstKey
{
get
{
return (FirstKey)firstKey;
}
set
{
firstKey = value;
}
}
public bool Enabled
{
get
{
return enabled;
}
set
{
enabled = value;
}
}
private bool IsFirstKey(Keys key)
{
if( FirstKey == FirstKey.Control )
{
return key == Keys.LControlKey || key == Keys.RControlKey;
}
else if( FirstKey == FirstKey.LeftAlt )
{
return key == Keys.LMenu;
}
else
{
return false;
}
}
private bool IsSecondKey(Keys key)
{
return key == Keys.LShiftKey || key == Keys.RShiftKey;
}
IntPtr KeyboardHook(int nCode, IntPtr wParam, IntPtr lParam)
{
if( nCode >= 0 )
{
KBDLLHOOKSTRUCT KeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
bool skip = false;
int oldKeyCount = (firstPressed ? 1 : 0) + (secondPressed ? 1 : 0);
if ( wParam.ToInt32() == WM_KEYDOWN && IsFirstKey(KeyInfo.keys) )
{
firstPressed = true;
}
else if (wParam.ToInt32() == WM_KEYDOWN && IsSecondKey(KeyInfo.keys))
{
secondPressed = true;
}
else if (wParam.ToInt32() == WM_KEYUP && IsFirstKey(KeyInfo.keys))
{
firstPressed = false;
}
else if (wParam.ToInt32() == WM_KEYUP && IsSecondKey(KeyInfo.keys))
{
secondPressed = false;
}
/*
else if (KeyInfo.keys == Keys.LControlKey || KeyInfo.keys == Keys.RControlKey || KeyInfo.keys == Keys.RShiftKey || KeyInfo.keys == Keys.LShiftKey)
{
// nothing
}
*/
else
{
skip = true;
}
if (!skip)
{
int newKeyCount = (firstPressed ? 1 : 0) + (secondPressed ? 1 : 0);
if(oldKeyCount==1 && newKeyCount==2)
{
FireNextTemporaryInputLanguage();
}
else if( oldKeyCount>0 && newKeyCount == 0 )
{
FireSwitchToTemporaryInputLanguage();
}
}
else if( firstPressed || secondPressed )
{
firstPressed = secondPressed = false;
FireDropTemporaryInputLanguage();
}
}
return CallNextHookEx(keyboardHookID, nCode, wParam, lParam);
}
public event NextTemporaryInputLanguageDelegate NextTemporaryInputLanguage;
private void FireNextTemporaryInputLanguage()
{
NextTemporaryInputLanguage?.Invoke();
}
public DropTemporaryInputLanguageDelegate DropTemporaryInputLanguage;
private void FireDropTemporaryInputLanguage()
{
DropTemporaryInputLanguage?.Invoke();
}
public SwitchToTemporaryInputLanguageDelegate SwitchToTemporaryInputLanguage;
private void FireSwitchToTemporaryInputLanguage()
{
SwitchToTemporaryInputLanguage?.Invoke();
}
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int VK_MENU = 0x12; // ALT key
private const int VK_LMENU = 0xA4;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardHookDelegate lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}