-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard.cs
631 lines (559 loc) · 21 KB
/
Keyboard.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WindowsInput;
using WindowsInput.Native;
using AcrylicKeyboard.Events;
using AcrylicKeyboard.Interaction;
using AcrylicKeyboard.Layout;
using AcrylicKeyboard.Layout.Sizes;
using AcrylicKeyboard.Renderer;
using AcrylicKeyboard.Renderer.Animation;
using AcrylicKeyboard.Theme;
namespace AcrylicKeyboard
{
public class Keyboard : IDisposable
{
public delegate void OnKeyActionDelegate(Keyboard sender, KeyActionEventArgs args);
public delegate void OnPopupOpenDelegate(Keyboard sender, PopupOpenEventArgs args);
public delegate void OnUpdateDelegate(Keyboard sender, double delta);
public delegate void OnRenderDelegate(Keyboard sender, DrawingContext context);
public delegate void OnResizeDelegate(Keyboard sender, ResizeEventArgs args);
public delegate void OnLayoutChangedDelegate(Keyboard sender, LayoutChangedEventArgs args);
public delegate void OnThemeChangedDelegate(Keyboard sender, ThemeChangedEventArgs args);
private readonly Grid panel;
private readonly DrawingCanvas canvas;
private readonly KeyboardRenderer keyboardRenderer;
private readonly KeyboardPopupRenderer popupRenderer;
private readonly Animator animator = new Animator();
private readonly List<KeyModifier> activeKeyModifiers = new List<KeyModifier>();
private readonly Dictionary<string, string> configurationFiles = new Dictionary<string, string>();
private readonly Dictionary<string, KeyboardLayoutConfig> keyboards =
new Dictionary<string, KeyboardLayoutConfig>();
private KeyboardTheme theme;
private string selectedLanguage;
private string selectedLayout;
private Size canvasSize;
private Rect keyboardBounds;
private int keyGap;
private bool hasInitedRenderers;
private bool freezeLayout;
private InputHandler inputHandler;
private InputSimulator inputSimulator;
private IKeyboardSizeResolver sizeResolver;
public event OnKeyActionDelegate OnKeyAction;
public event OnPopupOpenDelegate OnPopupOpen;
public event OnUpdateDelegate OnUpdate;
public event OnRenderDelegate OnRenderBefore;
public event OnRenderDelegate OnRenderAfter;
public event OnResizeDelegate OnResize;
public event OnLayoutChangedDelegate OnLayoutChanged;
public event OnThemeChangedDelegate OnThemeChanged;
public Keyboard(Grid panel)
{
this.panel = panel;
canvas = new DrawingCanvas(OnUpdateEvent, OnRenderEvent);
panel.Children.Clear();
panel.Children.Add(canvas);
Canvas.SizeChanged += OnSizeChanged;
Theme = default;
keyboardRenderer = new KeyboardRenderer(this);
popupRenderer = new KeyboardPopupRenderer(this);
InputHandler = new MouseInteractionHandler(this);
InputSimulator = new InputSimulator();
SizeResolver = new AspectRatioSizeResolver(3);
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (!freezeLayout)
{
canvasSize = e.NewSize;
if (SizeResolver != null)
{
var result = SizeResolver.ResolveSize(canvasSize);
keyboardBounds = result.bounds;
keyGap = result.gap;
}
else
{
keyboardBounds = new Rect(0, 0, canvasSize.Width, canvasSize.Height);
}
OnResize?.Invoke(this, new ResizeEventArgs(canvasSize, keyboardBounds));
InvalidateRenderer();
}
}
/// <summary>
/// Performs the action of the specified <see cref="KeyInstance" />.
/// The action is always dispatched on the Dispatcher thread.
/// </summary>
/// <param name="key">The <see cref="KeyInstance" /> of the action.</param>
/// <param name="isHolding">Determines whether or not the holding action should be invoked.</param>
public void PerformAction(KeyInstance key, bool isHolding)
{
if (panel.Dispatcher.CheckAccess())
{
InvokeAction();
}
else
{
panel.Dispatcher.Invoke(InvokeAction);
}
void InvokeAction()
{
if (key.Settings.KnownModifier != KeyModifier.None)
{
switch (key.Settings.KnownModifier)
{
case KeyModifier.Shift:
if (isHolding)
{
InputSimulator.Keyboard.KeyPress(VirtualKeyCode.CAPITAL);
}
else
{
ToggleModifier(KeyModifier.Shift);
if (WinApiHelper.IsCapsLock)
{
InputSimulator.Keyboard.KeyPress(VirtualKeyCode.CAPITAL);
DisableModifier(KeyModifier.Shift);
}
}
break;
case KeyModifier.Ctrl:
ToggleModifier(KeyModifier.Ctrl);
break;
case KeyModifier.Alt:
ToggleModifier(KeyModifier.Alt);
break;
}
}
if (isHolding && key.Settings.ExtraKeys != null)
{
var popupOpenArgs = new PopupOpenEventArgs();
OnPopupOpen?.Invoke(this, popupOpenArgs);
if (!popupOpenArgs.PreventOpening)
{
InputHandler.InteractionMode = InteractionMode.Popup;
PopupRenderer.ShowPopup(key);
}
}
else
{
var wasEventHandles = false;
var action = key.Settings.Action;
if (isHolding)
{
action = key.Settings.HoldingAction;
}
switch (action)
{
case KeyboardAction.InsertOnText:
if (key.Settings.KnownModifier == KeyModifier.None)
{
InsertText(GetKeyText(key.Settings, key.Settings.InsertionText));
wasEventHandles = true;
}
break;
case KeyboardAction.CursorLeft:
MoveCaret(-1);
break;
case KeyboardAction.CursorRight:
MoveCaret(1);
break;
case KeyboardAction.Delete:
DeleteText();
break;
case KeyboardAction.ReloadKeyboard:
ReloadKeyboards();
break;
case KeyboardAction.Enter:
PressReturnKey();
break;
case KeyboardAction.SwitchLayout:
SwitchLayout(SelectedLanguage, key.Settings.Target);
break;
case KeyboardAction.ChangeLanguage:
SwitchLayout(key.Settings.Target, SelectedLayout);
break;
}
if (!wasEventHandles)
{
OnKeyAction?.Invoke(this, new KeyActionEventArgs(action));
}
}
}
}
/// <summary>
/// Toggles a key role as modifier.
/// </summary>
/// <param name="modifier">The <see cref="KeyModifier" /> to toggle.</param>
private void ToggleModifier(KeyModifier modifier)
{
if (IsModifierActive(modifier))
{
DisableModifier(modifier);
}
else
{
ActivateModifier(modifier);
}
}
/// <summary>
/// Explicitly activates a key modifier.
/// </summary>
/// <param name="modifier">The <see cref="KeyModifier" /> to activate.</param>
private void ActivateModifier(KeyModifier modifier)
{
if (!activeKeyModifiers.Contains(modifier))
{
activeKeyModifiers.Add(modifier);
}
}
/// <summary>
/// Disables all active key modifiers.
/// </summary>
private void DisableAllModifiers()
{
activeKeyModifiers.Clear();
}
/// <summary>
/// Explicitly disables a key modifier.
/// </summary>
/// <param name="modifier">The <see cref="KeyModifier" /> to disable.</param>
private void DisableModifier(KeyModifier modifier)
{
activeKeyModifiers.Remove(modifier);
}
/// <summary>
/// Moves the cared by a specified amount.
/// </summary>
/// <param name="to">The amount of steps in a given direction. Below 0 is left.</param>
private void MoveCaret(int to)
{
var keycode = to < 0 ? VirtualKeyCode.LEFT : VirtualKeyCode.RIGHT;
to = Math.Abs(to);
for (var i = 0; i < to; i++)
{
InputSimulator.Keyboard.KeyPress(keycode);
}
}
/// <summary>
/// Simulates the insertion of a specified string.
/// </summary>
/// <param name="text">The string to insert.</param>
private void InsertText(string text)
{
if (text == null) return;
SetModifierKeyState(true);
InputSimulator.Keyboard.TextEntry(text);
OnKeyAction?.Invoke(this, new KeyActionEventArgs(KeyboardAction.InsertOnText, text, activeKeyModifiers));
SetModifierKeyState(false);
DisableAllModifiers();
}
/// <summary>
/// Simulates the press of the return key.
/// </summary>
private void PressReturnKey()
{
InputSimulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
DisableAllModifiers();
}
/// <summary>
/// Switches the layout to the given language and layout name.
/// </summary>
/// <param name="language">The registered language to switch to.</param>
/// <param name="layout">The registered layout to switch to.</param>
public void SwitchLayout(string language, string layout)
{
selectedLanguage = language;
selectedLayout = layout;
if (language != null && layout != null && keyboards.ContainsKey(language) &&
keyboards[language].Layouts.ContainsKey(layout))
{
InvalidateLayout();
}
InputHandler?.InvalidatePointerPosition();
}
/// <summary>
/// Invalidates the layout and invokes the event.
/// </summary>
private void InvalidateLayout()
{
OnLayoutChanged?.Invoke(this,
new LayoutChangedEventArgs(SelectedLanguage, SelectedLayout, GetLayoutConfig()));
}
/// <summary>
/// Simulates the press or release of a modifier key.
/// </summary>
/// <param name="down">Determines whether or not the simulator should press down.</param>
private void SetModifierKeyState(bool down)
{
void InvokeActionInternal(VirtualKeyCode key)
{
if (down)
{
InputSimulator.Keyboard.KeyDown(key);
}
else
{
InputSimulator.Keyboard.KeyUp(key);
}
}
foreach (var modifier in activeKeyModifiers)
{
switch (modifier)
{
case KeyModifier.Shift:
InvokeActionInternal(VirtualKeyCode.SHIFT);
break;
case KeyModifier.Ctrl:
InvokeActionInternal(VirtualKeyCode.CONTROL);
break;
case KeyModifier.Alt:
InvokeActionInternal(VirtualKeyCode.MENU);
break;
}
}
}
/// <summary>
/// Simulates a delete key press.
/// </summary>
private void DeleteText()
{
InputSimulator.Keyboard.KeyPress(VirtualKeyCode.BACK);
}
/// <summary>
/// Registers a file to a specified language.
/// </summary>
/// <param name="language">The language of the file.</param>
/// <param name="file">The file name.</param>
public void RegisterKeyboard(string language, string file)
{
language = language?.ToUpper();
if (language != null)
{
configurationFiles[language] = file;
LoadKeyboard(language, file);
}
}
/// <summary>
/// Reloads all registered keyboard files.
/// </summary>
public void ReloadKeyboards()
{
keyboards.Clear();
selectedLanguage = null;
selectedLayout = null;
foreach (var kvp in configurationFiles)
{
LoadKeyboard(kvp.Key, kvp.Value);
}
InvalidateLayout();
}
/// <summary>
/// Registers a single keyboard file specified by language.
/// </summary>
/// <param name="language">The language string.</param>
/// <param name="file">The file name.</param>
private void LoadKeyboard(string language, string file)
{
keyboards[language] = KeyboardLayoutConfig.FromFile(file);
if (selectedLanguage == null)
{
SwitchLayout(language, SelectedLayout);
}
if (selectedLayout == null)
{
var layout = GetLayoutConfig()?.Layouts?.FirstOrDefault().Key;
SwitchLayout(SelectedLanguage, layout);
}
}
private void OnUpdateEvent(double delta)
{
Animator.Update(delta);
NotifyRenderers(renderer => renderer.Update(this, delta));
KeyboardRenderer?.OnUpdate(delta);
PopupRenderer?.OnUpdate(delta);
OnUpdate?.Invoke(this, delta);
}
private void OnRenderEvent(DrawingContext context)
{
if (!hasInitedRenderers)
{
NotifyRenderers(renderer => renderer.Init(context));
hasInitedRenderers = true;
}
if (HasConfig)
{
OnRenderBefore?.Invoke(this, context);
KeyboardRenderer?.OnRender(context);
PopupRenderer?.OnRender(context);
OnRenderAfter?.Invoke(this, context);
}
}
/// <summary>
/// Invokes an action for all renderers.
/// </summary>
private void NotifyRenderers(Action<KeyRenderer> action)
{
if (Theme.FallbackRenderer != null)
{
action(Theme.FallbackRenderer);
}
foreach (var kvp in Theme.Renderers)
{
if (Theme.FallbackRenderer != kvp.Value)
{
action(kvp.Value);
}
}
}
/// <summary>
/// Gets the selected language;
/// </summary>
public string SelectedLanguage => selectedLanguage;
/// <summary>
/// Determines whether or not a specified <see cref="KeyModifier" /> is active.
/// </summary>
public bool IsModifierActive(KeyModifier modifier)
{
return activeKeyModifiers.Contains(modifier);
}
/// <summary>
/// Returns the current <see cref="KeyboardLayoutConfig" />.
/// </summary>
public KeyboardLayoutConfig GetLayoutConfig()
{
if (keyboards.TryGetValue(SelectedLanguage ?? "EN", out var result))
{
return result;
}
return null;
}
/// <summary>
/// Returns a upper or lower case variant of a specified string depending on an active
/// <see cref="KeyModifier.Shift" /> modifier.
/// If <see cref="KeySettings.IgnoreCap" /> is set to true, no changes will be made.
/// </summary>
/// <param name="settings">The keys settings.</param>
/// <param name="text">The string to transform.</param>
/// <returns>A lower or upper variant of the string.</returns>
public string GetKeyText(KeySettings settings, string text)
{
if (settings != null && !settings.IgnoreCap && text != null)
{
if (IsModifierActive(KeyModifier.Shift) || WinApiHelper.IsCapsLock)
{
return text.ToUpper();
}
return text.ToLower();
}
return text;
}
/// <summary>
/// Gets the selected layout.
/// </summary>
public string SelectedLayout => selectedLayout;
public void Dispose()
{
canvas.Dispose();
}
/// <summary>
/// Gets or sets the keyboard theme.
/// </summary>
public KeyboardTheme Theme
{
get => theme;
set
{
theme = value ?? KeyboardTheme.Default;
OnThemeChanged?.Invoke(this, new ThemeChangedEventArgs(theme));
InvalidateRenderer();
}
}
/// <summary>
/// Gets the <see cref="DrawingCanvas" />.
/// </summary>
public DrawingCanvas Canvas => canvas;
/// <summary>
/// Gets the keyboard bounds.
/// </summary>
public Rect KeyboardBounds => keyboardBounds;
/// <summary>
/// Determines whether or not the keyboard has a selected <see cref="KeyboardLayoutConfig" />.
/// </summary>
private bool HasConfig => SelectedLanguage != null && keyboards.ContainsKey(SelectedLanguage);
/// <summary>
/// Gets or sets the <see cref="InputHandler" />.
/// </summary>
public InputHandler InputHandler
{
get => inputHandler;
set
{
inputHandler = value;
inputHandler?.Init();
}
}
/// <summary>
/// Gets the key gap.
/// </summary>
public int KeyGap => keyGap;
/// <summary>
/// Gets the <see cref="Animator" />.
/// </summary>
public Animator Animator => animator;
/// <summary>
/// Gets the <see cref="KeyboardRenderer" />.
/// </summary>
public KeyboardRenderer KeyboardRenderer => keyboardRenderer;
/// <summary>
/// Gets the <see cref="KeyboardPopupRenderer" />.
/// </summary>
public KeyboardPopupRenderer PopupRenderer => popupRenderer;
/// <summary>
/// Gets or sets the <see cref="InputSimulator" />.
/// </summary>
public InputSimulator InputSimulator
{
get => inputSimulator;
set => inputSimulator = value;
}
/// <summary>
/// Gets or sets the <see cref="SizeResolver" />.
/// </summary>
public IKeyboardSizeResolver SizeResolver
{
get => sizeResolver;
set => sizeResolver = value;
}
/// <summary>
/// Determinies whether or not the resize event should trigger a render cycle.
/// </summary>
public bool FreezeLayout
{
get => freezeLayout;
set
{
if (freezeLayout != value)
{
freezeLayout = value;
if (!freezeLayout)
{
InvalidateRenderer();
}
}
}
}
/// <summary>
/// Invalidates the canvas and forces redraw.
/// </summary>
public void InvalidateRenderer()
{
Canvas?.InvalidateRender();
}
}
}