forked from Phaiax/Key-n-Stroke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeystrokeParser.cs
414 lines (369 loc) · 15.9 KB
/
KeystrokeParser.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using KeyNStroke;
namespace KeyNStroke
{
public class KeystrokeParser : IKeystrokeEventProvider
{
//KeysConverter Converter = new KeysConverter();
#region Constructor
public KeystrokeParser(IKeyboardRawEventProvider hook)
{
hook.KeyEvent += hook_KeyEvent;
}
#endregion
/// <summary>
/// Gets a KeyboardRawKeyEvent, parses it and forwards it via
/// the KeystrokeEvent
/// </summary>
/// <param name="e"></param>
void hook_KeyEvent(KeyboardRawEventArgs raw_e)
{
KeystrokeEventArgs e = new KeystrokeEventArgs(raw_e);
e.IsAlpha = CheckIsAlpha(e);
e.IsNumericFromNumpad = CheckIsNumericFromNumpad(e);
e.IsNumericFromNumbers = CheckIsNumericFromNumbers(e);
e.IsNoUnicodekey = CheckIsNoUnicodekey(e);
e.IsFunctionKey = CheckIsFunctionKey(e);
e.ModifierToggledEvent = CheckKeyIsModifier(e);
Log.e("KP", " alpha:" + e.IsAlpha.ToString());
if (e.Method == KeyUpDown.Down)
{
ApplyDeadKey(e);
if (e.IsAlpha && e.OnlyShiftOrCaps)
{
e.KeyString = ParseChar(e);
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Text;
e.Deletable = true;
Log.e("KP", " IsAlpha and OnlyShiftOrCaps > ParseChar");
}
else if (e.IsNumeric && e.NoModifiers)
{
e.KeyString = ParseNumeric(e);
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Text;
e.Deletable = true;
Log.e("KP", " e.IsNumeric && e.NoModifiers > ParseNumeric");
}
else if (e.ModifierToggledEvent) // key is modifier
{
e.ShouldBeDisplayed = false;
AddModifier(e.Key, e);
e.StrokeType = KeystrokeType.Modifiers;
Log.e("KP", " e.ModifierToggledEvent > AddModifier " + e.Key.ToString());
}
else if (e.IsNoUnicodekey && e.NoModifiers)
{
ParseTexttViaSpecialkeysParser(e);
e.Deletable = IsDeletableSpecialKey(e.Key);
Log.e("KP", " e.IsNoUnicodekey && e.NoModifiers > ParseTexttViaSpecialkeysParser ");
}
else if (e.IsNoUnicodekey && !e.NoModifiers) // Shortcut
{
ParseShortcutViaSpecialkeysParser(e);
Log.e("KP", " e.IsNoUnicodekey && !e.NoModifiers > ParseShortcutViaSpecialkeysParser ");
}
else if (e.NoModifiers) // Simple Key, but not alphanumeric (first try special then unicode)
{
Log.e("KP", " e.NoModifiers > try SpecialkeysParser.ToString ");
try
{
e.KeyString = SpecialkeysParser.ToString(e.Key);
}
catch (NotImplementedException)
{
Log.e("KP", " e.NoModifiers 2> try KeyboardLayoutParser.ParseViaToUnicode ");
e.KeyString = KeyboardLayoutParser.ParseViaToUnicode(e);
BackupDeadKey(e);
}
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Text;
e.RequiresNewLineAfterwards = e.Key == Key.Return;
}
else if (e.OnlyShiftOrCaps) // special char, but only Shifted, eg ;:_ÖÄ'*ÜP
// (e.IsNoUnicodekey is always false here -> could be a unicode key combinatin)
{
e.KeyString = KeyboardLayoutParser.ParseViaToUnicode(e);
BackupDeadKey(e);
Log.e("KP", " e.OnlyShiftOrCaps > try KeyboardLayoutParser.ParseViaToUnicode ");
if (e.KeyString != "")
{
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Text;
e.Deletable = true;
}
else
{
// Is no unicode key combination? maybe a shortcut then: Shift + F2
ParseShortcutViaSpecialkeysParser(e);
Log.e("KP", " e.OnlyShiftOrCaps 2> ParseShortcutViaSpecialkeysParser ");
}
}
else if (!e.NoModifiers && !e.OnlyShiftOrCaps) // Special Char with Strg + Alt
// or shortcut else
{
// could be something like the german @ (Ctrl + Alt + Q)
// Temporary disabled because ToUnicode returns more often values than it should
e.KeyString = ""; //KeyboardLayoutParser.ParseViaToUnicode(e);
Log.e("KP", " !e.NoModifiers && !e.OnlyShiftOrCapss > KeyboardLayoutParser.ParseViaToUnicode");
// all other special char keycodes do not use Shift
if (e.KeyString != "" && !e.Shift && !e.IsNoUnicodekey)
{
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Text;
e.Deletable = true;
}
else // Shortcut
{
ParseShortcutViaSpecialkeysParser(e);
string possibleChar = KeyboardLayoutParser.ParseViaToUnicode(e);
BackupDeadKey(e);
if (possibleChar != "" && !CheckIsAlpha(possibleChar)
&& !CheckIsNumeric(possibleChar)
&& CheckIsASCII(possibleChar))
{
e.KeyString += " (" + possibleChar + ")";
}
Log.e("KP", " !e.NoModifiers && !e.OnlyShiftOrCapss 2> ParseShortcutViaSpecialkeysParser ");
}
}
Log.e("KP", " str:" + e.KeyString);
}
if (e.Method == KeyUpDown.Up)
{
if (e.ModifierToggledEvent) // key is modifier
{
e.ShouldBeDisplayed = false;
RemoveModifier(e.Key, e);
e.StrokeType = KeystrokeType.Modifiers;
}
Log.e("KP", " code:" + (e.Key).ToString());
// only react to modifiers on key up, nothing else
}
if (e.StrokeType != KeystrokeType.Undefined)
{
OnKeystrokeEvent(e);
}
}
private KeystrokeEventArgs lastDeadKeyEvent;
private void BackupDeadKey(KeystrokeEventArgs e)
{
if(e.KeyString == "DEADKEY")
{
e.KeyString = "!";
lastDeadKeyEvent = e;
}
}
private bool ApplyDeadKey(KeystrokeEventArgs e)
{
if(lastDeadKeyEvent != null)
{
lastDeadKeyEvent.KeyString = KeyboardLayoutParser.ProcessDeadkeyWithNextKey(lastDeadKeyEvent, e);
if(lastDeadKeyEvent.KeyString == "")
{
lastDeadKeyEvent = null;
return false;
}
OnKeystrokeEvent(lastDeadKeyEvent);
lastDeadKeyEvent = null;
return true;
}
return false;
}
private void ParseShortcutViaSpecialkeysParser(KeystrokeEventArgs e)
{
try
{
e.KeyString = SpecialkeysParser.ToString(e.Key);
}
catch (NotImplementedException)
{
e.KeyString = e.Key.ToString();
}
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Shortcut;
e.RequiresNewLine = true;
e.RequiresNewLineAfterwards = true;
}
private void ParseTexttViaSpecialkeysParser(KeystrokeEventArgs e)
{
try
{
e.KeyString = SpecialkeysParser.ToString(e.Key);
e.ShouldBeDisplayed = true;
e.StrokeType = KeystrokeType.Text;
e.RequiresNewLineAfterwards = e.Key == Key.Return;
}
catch (NotImplementedException)
{
}
}
private void AddModifier(Key keys, KeystrokeEventArgs e)
{
switch (keys)
{
case Key.LeftCtrl: e.LCtrl = true; e.Ctrl = true; break;
case Key.RightCtrl: e.RCtrl = true; e.Ctrl = true; break;
case Key.LeftShift: e.LShift = true; e.Shift = true; break;
case Key.RightShift: e.RShift = true; e.Shift = true; break;
case Key.LWin: e.LWin = true; break;
case Key.RWin: e.RWin = true; break;
case Key.LeftAlt: e.LAlt = true; e.Alt = true; break;
case Key.RightAlt: e.RAlt = true; e.Alt = true; break;
case Key.NumLock: e.Numlock = true; break;
case Key.Scroll: e.Scrollock = true; break;
case Key.CapsLock: e.Caps = true; break;
}
}
private void RemoveModifier(Key keys, KeystrokeEventArgs e)
{
// If Left Shift is released, then only unset Shift if RShift is not set
switch (keys)
{
case Key.LeftCtrl: e.LCtrl = false; e.Ctrl = e.RCtrl; break;
case Key.RightCtrl: e.RCtrl = false; e.Ctrl = e.LCtrl; break;
case Key.LeftShift: e.LShift = false; e.Shift = e.RShift; break;
case Key.RightShift: e.RShift = false; e.Shift = e.LShift; break;
case Key.LWin: e.LWin = false; break;
case Key.RWin: e.RWin = false; break;
case Key.LeftAlt: e.LAlt = false; e.Alt = e.RAlt; break;
case Key.RightAlt: e.RAlt = false; e.Alt = e.LAlt; break;
case Key.NumLock: e.Numlock = false; break;
case Key.Scroll: e.Scrollock = false; break;
case Key.CapsLock: e.Caps = false; break;
}
}
private string ParseChar(KeystrokeEventArgs e)
{
string c = e.Key.ToString();
if (!e.Uppercase)
c = c.ToLower();
// We parse the Shift and Caps keys into the Upper/Lowercase
e.Shift = false;
e.Caps = false;
e.LShift = false;
e.RShift = false;
return c;
}
private string ParseNumeric(KeystrokeEventArgs e)
{
if (e.IsNumericFromNumbers)
{
return ((int)e.Key - (int)Key.D0).ToString();
}
else if (e.IsNumericFromNumpad)
{
return ((int)e.Key - (int)Key.NumPad0).ToString();
}
return "BUG1";
}
bool CheckIsAlpha(KeyboardRawEventArgs e)
{
return CheckIsAlpha(e.Key);
}
bool CheckIsAlpha(Key key)
{
return ((int)Key.A <= (int)key && (int)key <= (int)Key.Z);
}
bool CheckIsAlpha(string s)
{
return (s.Length == 1 && ( (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z')));
}
bool CheckIsASCII(string s)
{
return (s.Length == 1 && ((s[0] >= 32 && s[0] <= 126) ));
}
bool CheckIsNumericFromNumpad(KeyboardRawEventArgs e)
{
return CheckIsNumericFromNumpad(e.Key);
}
bool CheckIsNumericFromNumpad(Key key)
{
return ((int)Key.NumPad0 <= (int)key && (int)key <= (int)Key.NumPad9);
}
bool CheckIsNumeric(string s)
{
return (s.Length == 1 && (s[0] >= '0' && s[0] <= '9'));
}
bool CheckIsNumericFromNumbers(KeyboardRawEventArgs e)
{
return CheckIsNumericFromNumbers(e.Key);
}
bool CheckIsNumericFromNumbers(Key key)
{
return ((int)Key.D0 <= (int)key && (int)key <= (int)Key.D9);
}
bool CheckIsFunctionKey(KeyboardRawEventArgs e)
{
return ((int)Key.F1 <= (int)e.Key && (int)e.Key <= (int)Key.F24);
}
/// <summary>
/// If this function returns true, there will be no attemt to decode the key press
/// via ToUnicode to reveal Strg+Alt+E = € .
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
bool CheckIsNoUnicodekey(KeyboardRawEventArgs e)
{
Key[] NoUnicodeKeys = { Key.Cancel, Key.Tab, Key.LineFeed, Key.Clear,
Key.Enter, Key.Return,
Key.Pause, Key.CapsLock, Key.Capital, Key.KanaMode,
Key.JunjaMode, Key.FinalMode, Key.KanjiMode, Key.HanjaMode,
Key.Escape, Key.ImeConvert, Key.ImeNonConvert, Key.ImeAccept,
Key.ImeModeChange, Key.Space, Key.Prior, Key.PageUp, Key.Next,
Key.PageDown, Key.End, Key.Home, Key.Left, Key.Up, Key.Right,
Key.Down, Key.Select, Key.Print, Key.Execute, Key.PrintScreen,
Key.Snapshot, Key.Insert, Key.Delete, Key.Help,
Key.LWin, Key.RWin, Key.Apps, Key.Sleep,
Key.Multiply, Key.Add, Key.Separator, Key.Subtract, Key.Decimal,
Key.Divide, Key.F1, Key.F2, Key.F3, Key.F4, Key.F5, Key.F6,
Key.F7, Key.F8, Key.F9, Key.F10, Key.F11, Key.F12, Key.F13,
Key.F14, Key.F15, Key.F16, Key.F17, Key.F18, Key.F19, Key.F20,
Key.F21, Key.F22, Key.F23, Key.F24, Key.NumLock, Key.Scroll,
Key.LeftShift, Key.RightShift, Key.LeftCtrl, Key.RightCtrl,
Key.LeftAlt, Key.RightAlt, Key.BrowserBack, Key.BrowserForward,
Key.BrowserRefresh, Key.BrowserStop, Key.BrowserSearch,
Key.BrowserFavorites, Key.BrowserHome, Key.VolumeMute, Key.VolumeDown,
Key.VolumeUp, Key.MediaNextTrack, Key.MediaPreviousTrack, Key.MediaStop,
Key.MediaPlayPause, Key.LaunchMail, Key.SelectMedia, Key.LaunchApplication1,
Key.LaunchApplication2, Key.ImeProcessed,
0, Key.Attn, Key.CrSel, Key.ExSel, Key.EraseEof, Key.Play,
Key.Zoom, Key.NoName, Key.Pa1, Key.OemClear };
return NoUnicodeKeys.Contains(e.Key);
}
private bool IsDeletableSpecialKey(Key key)
{
Key[] DeletableKeys = {
Key.Multiply, Key.Add, Key.Subtract, Key.Decimal,
Key.Divide, Key.Space};
return DeletableKeys.Contains(key);
}
bool CheckKeyIsModifier(KeyboardRawEventArgs e)
{
Key[] ModifierKeys = { Key.LeftShift, Key.RightShift,
Key.LeftCtrl, Key.RightCtrl,
Key.LeftAlt, Key.RightAlt,
Key.LWin, Key.RWin,
Key.NumLock, Key.Scroll,
Key.CapsLock};
return ModifierKeys.Contains(e.Key);
}
#region Event Forwarding
public event KeystrokeEventHandler KeystrokeEvent;
private void OnKeystrokeEvent(KeystrokeEventArgs e)
{
if (KeystrokeEvent != null)
{
KeystrokeEvent(e);
}
}
#endregion
}
}