-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathOSK.xaml.cs
853 lines (693 loc) · 29.8 KB
/
OSK.xaml.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using Power_Control_Panel.PowerControlPanel.Classes.RoutineUpdate;
using WindowsInput.Native;
using SharpDX.XInput;
using WindowsInput;
using System.Runtime.InteropServices;
using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;
using Power_Control_Panel.PowerControlPanel.Classes;
using System.Diagnostics;
using ControlzEx.Theming;
using System.Windows.Interop;
namespace Power_Control_Panel
{
/// <summary>
/// Interaction logic for OSK.xaml
/// </summary>
public partial class OSK : MetroWindow
{
WindowSinker sinker;
InputSimulator sim = new InputSimulator();
private Controller controller;
private Gamepad gamepadOld;
private Gamepad gamepadCurrent;
private const double CircleWidth = 10;
//private Dictionary<int, Point> TouchPositions = new Dictionary<int, Point>();
//private Dictionary<int, Ellipse> TouchEllipses = new Dictionary<int, Ellipse>();
private double LLx = CircleWidth / 2;
private double LLy = CircleWidth / 2;
private double ULx = System.Windows.SystemParameters.PrimaryScreenWidth - CircleWidth / 2;
private double ULy;
private double windowY;
private bool ellipseSetup = false;
private Ellipse ellipseL;
private Ellipse ellipseR;
private Point pointL;
private Point pointR;
private bool keyAlt = false;
private bool keyCtrl = false;
private bool keyCap = false;
private bool keyShift = false;
private bool keyWin = false;
buttonEvents events = new buttonEvents();
Dictionary<string, VirtualKeyCode> keyLookUp =
new Dictionary<string, VirtualKeyCode>()
{
{"T_A", VirtualKeyCode.VK_A },
{"T_B", VirtualKeyCode.VK_B },
{"T_C", VirtualKeyCode.VK_C },
{"T_D", VirtualKeyCode.VK_D },
{"T_E", VirtualKeyCode.VK_E },
{"T_F", VirtualKeyCode.VK_F },
{"T_G", VirtualKeyCode.VK_G },
{"T_H", VirtualKeyCode.VK_H },
{"T_I", VirtualKeyCode.VK_I },
{"T_J", VirtualKeyCode.VK_J },
{"T_K", VirtualKeyCode.VK_K },
{"T_L", VirtualKeyCode.VK_L },
{"T_M", VirtualKeyCode.VK_M },
{"T_N", VirtualKeyCode.VK_N },
{"T_O", VirtualKeyCode.VK_O },
{"T_P", VirtualKeyCode.VK_P },
{"T_Q", VirtualKeyCode.VK_Q },
{"T_R", VirtualKeyCode.VK_R },
{"T_S", VirtualKeyCode.VK_S },
{"T_T", VirtualKeyCode.VK_T },
{"T_U", VirtualKeyCode.VK_U },
{"T_V", VirtualKeyCode.VK_V },
{"T_W", VirtualKeyCode.VK_W },
{"T_X", VirtualKeyCode.VK_X },
{"T_Y", VirtualKeyCode.VK_Y },
{"T_Z", VirtualKeyCode.VK_Z },
{"T_1", VirtualKeyCode.VK_1 },
{"T_2", VirtualKeyCode.VK_2 },
{"T_3", VirtualKeyCode.VK_3 },
{"T_4", VirtualKeyCode.VK_4 },
{"T_5", VirtualKeyCode.VK_5 },
{"T_6", VirtualKeyCode.VK_6 },
{"T_7", VirtualKeyCode.VK_7 },
{"T_8", VirtualKeyCode.VK_8 },
{"T_9", VirtualKeyCode.VK_9 },
{"T_0", VirtualKeyCode.VK_0 },
{"T_Period", VirtualKeyCode.OEM_PERIOD },
{"T_Comma", VirtualKeyCode.OEM_COMMA },
{"T_Period_", VirtualKeyCode.OEM_PERIOD },
{"T_Comma_", VirtualKeyCode.OEM_COMMA },
};
private System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0010;
const UInt32 SWP_SHOWWINDOW = 0x0040;
// Call this way:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
public OSK()
{
InitializeComponent();
//Make window sinker, always on top, situate bottom of screen
setUpWindow();
//Set up controller events for button presses
setUpControllerEvents();
//Set up circles, make the shared ellipses to get ready to add to canvas
setUpCircles();
//check if controller is connected
setUpController();
//add circles to canvas if controller connected
addCirclesCanvas();
//Setup dispatcher timer
setupTimer();
//swap upper and lower case letters
swapAlphaUpperLower(false);
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
SetWindowPos(windowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
#region Controller press events
void HandlePressLBEvent(object sender, EventArgs a)
{
handleButtonPress(pointL, ellipseL);
}
void HandlePressRBEvent(object sender, EventArgs a)
{
handleButtonPress(pointR, ellipseR);
}
void HandlePressBEvent(object sender, EventArgs a)
{
Close();
}
void HandlePressYEvent(object sender, EventArgs a)
{
keyboardPress(canvMain.FindName("R_Space"));
}
void HandlePressXEvent(object sender, EventArgs a)
{
keyboardPress(canvMain.FindName("R_BckSpce"));
}
void HandlePressLTEvent(object sender, EventArgs a)
{
keyboardPress(canvMain.FindName("R_CAP"));
}
void HandlePressRTEvent(object sender, EventArgs a)
{
keyboardPress(canvMain.FindName("R_AlphaNum"));
}
#endregion controler press events
#region joystick point math
private double offset_calculator(short Input)
{
//Convert short from joystick to double, divide by largest value, and round to largest nubmer away from zero
if (Input < 500 && Input > 0)
{
return 1;
}
else
{
return Math.Round(10 * Convert.ToDouble(Input) / 32768, 0, MidpointRounding.AwayFromZero);
}
}
private Point offset_point(Point mp, Double dx, Double dy)
{
if ((mp.X + dx >= LLx) && (mp.X + dx <= ULx))
{
mp.Offset(dx, 0);
}
else
{
if (mp.X + dx < LLx)
{
mp.X = LLx;
}
else
{
mp.X = ULx;
}
}
if ((mp.Y + dy >= LLy) && (mp.Y + dy <= ULy))
{
mp.Offset(0, dy);
}
else
{
if (mp.Y + dy < LLy)
{
mp.Y = LLy;
}
else
{
mp.Y = ULy;
}
}
return mp;
}
#endregion joystick point math
#region setup routines
private void setUpControllerEvents()
{
events.pressBEvent += HandlePressBEvent;
events.pressXEvent += HandlePressXEvent;
events.pressYEvent += HandlePressYEvent;
events.pressLBEvent += HandlePressLBEvent;
events.pressRBEvent += HandlePressRBEvent;
events.pressLTEvent += HandlePressLTEvent;
events.pressRTEvent += HandlePressRTEvent;
}
private void setUpWindow()
{
//create window sinker for always on top
sinker = new WindowSinker(this);
sinker.Sink();
//more window options
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight * 0.4;
this.ShowInTaskbar = false;
this.Topmost = true;
this.WindowStyle = WindowStyle.None;
this.AllowsTransparency = true;
this.Left = 0;
this.Top = System.Windows.SystemParameters.PrimaryScreenHeight - this.Height;
ULy = this.Top - CircleWidth / 2;
windowY = this.Top;
}
private void setUpController()
{
//set up controller and get first gamepad state
controller = new Controller(UserIndex.One);
if (controller != null)
{
if (controller.IsConnected)
{
try
{
gamepadCurrent = controller.GetState().Gamepad;
}
catch { }
}
else { controller = null; }
}
}
private void setupTimer()
{
dispatcherTimer.Tick += new EventHandler(timer_Tick);
//If controller is not null and connected, make timespan 15 ms otherwise make it 3 seconds
if (controller != null) { if (controller.IsConnected) { dispatcherTimer.Interval = TimeSpan.FromMilliseconds(15); } else { dispatcherTimer.Interval = new TimeSpan(0, 0, 3); } } else { dispatcherTimer.Interval = new TimeSpan(0, 0, 3); }
dispatcherTimer.Start();
}
private void setUpCircles()
{
//Set up ellipses as circles to add to canvas
ellipseL = new Ellipse();
ellipseL.Stroke = Brushes.Black;
ellipseL.Fill = Brushes.Red;
ellipseL.Width = CircleWidth;
ellipseL.Height = CircleWidth;
ellipseR = new Ellipse();
ellipseR.Stroke = Brushes.Black;
ellipseR.Fill = Brushes.Blue;
ellipseR.Width = CircleWidth;
ellipseR.Height = CircleWidth;
}
#endregion set up routines
private void addCirclesCanvas()
{
if (controller != null)
{
if (controller.IsConnected)
{
if (!canvMain.Children.Contains(ellipseL))
{
pointL = new Point(this.Width * 0.25, this.Height * 0.5);
AddEllipseAt(canvMain, pointL, ellipseL);
}
if (!canvMain.Children.Contains(ellipseR))
{
pointR = new Point(this.Width * 0.75, this.Height * 0.5);
AddEllipseAt(canvMain, pointR, ellipseR);
}
ellipseSetup = true;
}
}
}
private void removeCircles()
{
if (canvMain.Children.Contains(ellipseL))
{
canvMain.Children.Remove(ellipseL);
}
if (canvMain.Children.Contains(ellipseR))
{
canvMain.Children.Remove(ellipseR);
}
}
private void AddEllipseAt(Canvas canv, Point pt, Ellipse ellipse)
{
Canvas.SetLeft(ellipse, pt.X - (CircleWidth / 2));
Canvas.SetTop(ellipse, pt.Y - (CircleWidth / 2));
canv.Children.Add(ellipse);
}
private void timer_Tick(object sender, EventArgs e)
{
//Check controller, if null start it
if (controller == null)
{
controller = new Controller(UserIndex.One);
if (controller == null)
{
dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
}
else
{
if (controller.IsConnected)
{
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(15);
addCirclesCanvas();
//Use try catch to prevent error when controller disconnects
try
{
gamepadCurrent = controller.GetState().Gamepad;
}
catch { }
}
else { dispatcherTimer.Interval = new TimeSpan(0, 0, 3); }
}
}
else
{
if (!controller.IsConnected)
{
controller = null;
ellipseSetup = false;
removeCircles();
}
}
if (controller != null)
{
if (controller.IsConnected)
{
//caputre in try to prevent error if controller disconnected
try
{
//get gamepad states
gamepadOld = gamepadCurrent;
gamepadCurrent = controller.GetState().Gamepad;
//move circles based on joystick movement
double dlx = offset_calculator(gamepadCurrent.LeftThumbX);
double dly = -1 * offset_calculator(gamepadCurrent.LeftThumbY);
double drx = offset_calculator(gamepadCurrent.RightThumbX);
double dry = -1 * offset_calculator(gamepadCurrent.RightThumbY);
pointL = offset_point(pointL, dlx, dly);
Canvas.SetLeft(ellipseL, pointL.X - (CircleWidth / 2));
Canvas.SetTop(ellipseL, pointL.Y - (CircleWidth / 2));
pointR = offset_point(pointR, drx, dry);
Canvas.SetLeft(ellipseR, pointR.X - (CircleWidth / 2));
Canvas.SetTop(ellipseR, pointR.Y - (CircleWidth / 2));
//check for event firing from button states
if (!gamepadOld.Buttons.HasFlag(GamepadButtonFlags.A) && gamepadCurrent.Buttons.HasFlag(GamepadButtonFlags.A))
{
events.RaiseEventA();
}
if (!gamepadOld.Buttons.HasFlag(GamepadButtonFlags.B) && gamepadCurrent.Buttons.HasFlag(GamepadButtonFlags.B))
{
events.RaiseEventB();
}
if (!gamepadOld.Buttons.HasFlag(GamepadButtonFlags.X) && gamepadCurrent.Buttons.HasFlag(GamepadButtonFlags.X))
{
events.RaiseEventX();
}
if (!gamepadOld.Buttons.HasFlag(GamepadButtonFlags.Y) && gamepadCurrent.Buttons.HasFlag(GamepadButtonFlags.Y))
{
events.RaiseEventY();
}
if (!gamepadOld.Buttons.HasFlag(GamepadButtonFlags.LeftShoulder) && gamepadCurrent.Buttons.HasFlag(GamepadButtonFlags.LeftShoulder))
{
events.RaiseEventLB();
}
if (!gamepadOld.Buttons.HasFlag(GamepadButtonFlags.RightShoulder) && gamepadCurrent.Buttons.HasFlag(GamepadButtonFlags.RightShoulder))
{
events.RaiseEventRB();
}
if (gamepadOld.RightTrigger == 0 && gamepadCurrent.RightTrigger > 0)
{
events.RaiseEventRT();
}
if (gamepadOld.LeftTrigger == 0 && gamepadCurrent.LeftTrigger > 0)
{
events.RaiseEventLT();
}
}
catch { }
}
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
keyboardPress(sender);
}
void swapAlphaUpperLower(bool upper)
{
Regex upperCaseRegex = new Regex("[A-Z]");
Regex lowerCaseRegex = new Regex("[a-z]");
foreach (UIElement elem in AlphaKB.Children) //iterate the main grid
{
if (elem is TextBlock) // if button contains only 1 character
{
TextBlock txtblk = elem as TextBlock;
if (txtblk.Text.Length == 1)
{
if (upperCaseRegex.Match(txtblk.Text.ToString()).Success && !upper) // if the char is a letter and uppercase
{
txtblk.Text = txtblk.Text.ToString().ToLower();
}
else if (lowerCaseRegex.Match(txtblk.Text.ToString()).Success && upper) // if the char is a letter and lower case
{
txtblk.Text = txtblk.Text.ToString().ToUpper();
}
}
}
}
}
void keyboardPress(object sender)
{
TextBlock txtblk;
string textValue = null;
string textName;
if (sender is Rectangle)
{
Rectangle rect = (Rectangle)sender;
sender = canvMain.FindName("T_" + rect.Name.Substring(2, rect.Name.Length - 2));
}
if (sender is TextBlock)
{
txtblk = (TextBlock)sender;
textValue = txtblk.Text;
textName = txtblk.Name;
bool isKeyDown;
if (textName == "")
{
txtblk = getParentTextBlock(txtblk);
textValue = txtblk.Text;
textName = txtblk.Name;
}
if (textName == "T_HideKeyboard") { this.Close(); }
else
{
if (textValue != "")
{
switch (textValue)
{
case "Esc":
sim.Keyboard.KeyPress(VirtualKeyCode.ESCAPE);
break;
case "Shift":
keyShift = !keyShift;
isKeyDown = sim.InputDeviceState.IsKeyDown(VirtualKeyCode.SHIFT);
if (isKeyDown) { sim.Keyboard.KeyUp(VirtualKeyCode.SHIFT); } else { sim.Keyboard.KeyDown(VirtualKeyCode.SHIFT); }
toggleRectangleOn(textName);
break;
case "Alt":
keyAlt = !keyAlt;
isKeyDown = sim.InputDeviceState.IsKeyDown(VirtualKeyCode.MENU);
if (isKeyDown) { sim.Keyboard.KeyUp(VirtualKeyCode.MENU); } else { sim.Keyboard.KeyDown(VirtualKeyCode.MENU); }
toggleRectangleOn(textName);
break;
case "Ctrl":
keyCtrl = !keyCtrl;
isKeyDown = sim.InputDeviceState.IsKeyDown(VirtualKeyCode.CONTROL);
if (isKeyDown) { sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL); } else { sim.Keyboard.KeyDown(VirtualKeyCode.CONTROL); }
toggleRectangleOn(textName);
break;
case "123":
AlphaKB.Visibility = Visibility.Hidden;
NumberKB.Visibility = Visibility.Visible;
txtblk.Text = "ABC";
break;
case "ABC":
AlphaKB.Visibility = Visibility.Visible;
NumberKB.Visibility = Visibility.Hidden;
txtblk.Text = "123";
break;
case "!":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_1);
break;
case "@":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_2);
break;
case "#":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_3);
break;
case "$":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_4);
break;
case "%":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_5);
break;
case "^":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_6);
break;
case "&":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_7);
break;
case "*":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_8);
break;
case "(":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_9);
break;
case ")":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.VK_0);
break;
case "-":
sim.Keyboard.TextEntry("-");
break;
case "/":
sim.Keyboard.TextEntry("/");
break;
case "+":
sim.Keyboard.TextEntry("+");
break;
case "=":
sim.Keyboard.TextEntry("=");
break;
case "Ctrl+A":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_A);
break;
case "Ctrl+C":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
break;
case "Ctrl+X":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_X);
break;
case "Ctrl+V":
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
break;
case "PrintScreen":
sim.Keyboard.KeyPress(VirtualKeyCode.PRINT);
break;
case "Del":
sim.Keyboard.KeyPress(VirtualKeyCode.DELETE);
break;
default:
VirtualKeyCode vkc;
vkc = keyLookUp[textName];
sim.Keyboard.KeyPress(vkc);
//sim.Keyboard.TextEntry(textValue);
break;
}
}
else
{
switch (txtblk.Name)
{
case "T_HideKeyboard":
this.Close();
break;
case "T_CAP":
isKeyDown = Console.CapsLock;
if (isKeyDown) { sim.Keyboard.KeyPress(VirtualKeyCode.CAPITAL); swapAlphaUpperLower(false); } else { sim.Keyboard.KeyPress(VirtualKeyCode.CAPITAL); swapAlphaUpperLower(true); }
toggleRectangleOn(textName);
break;
case "T_BckSpce":
sim.Keyboard.KeyPress(VirtualKeyCode.BACK);
break;
case "T_Enter":
sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
break;
case "T_Win":
sim.Keyboard.KeyPress(VirtualKeyCode.LWIN);
break;
case "T_Space":
sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
break;
}
}
//Clear ctrl alt shift win if used
if (textName != "T_Shift" && textName != "T_Alt" && textName != "T_Ctrl" )
{
isKeyDown = sim.InputDeviceState.IsKeyDown(VirtualKeyCode.SHIFT);
if (isKeyDown) { sim.Keyboard.KeyUp(VirtualKeyCode.SHIFT); toggleRectangleOn("T_Shift"); }
isKeyDown = sim.InputDeviceState.IsKeyDown(VirtualKeyCode.MENU);
if (isKeyDown) { sim.Keyboard.KeyUp(VirtualKeyCode.MENU); toggleRectangleOn("T_Alt"); }
isKeyDown = sim.InputDeviceState.IsKeyDown(VirtualKeyCode.CONTROL);
if (isKeyDown) { sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL); toggleRectangleOn("T_Ctrl"); }
}
}
}
}
void toggleRectangleOn(string txtblkName)
{
TextBlock txtblk = (TextBlock)canvMain.FindName(txtblkName);
Rectangle rect;
if (txtblk != null)
{
rect = (Rectangle)canvMain.FindName("R_" + txtblk.Name.Substring(2, txtblk.Name.Length - 2));
if (rect.Fill == Brushes.WhiteSmoke) { rect.Fill = Brushes.Gray; } else { rect.Fill = Brushes.WhiteSmoke; }
}
}
TextBlock getParentTextBlock(DependencyObject txtblk)
{
TextBlock returnTxtblk = null;
DependencyObject parent;
if (txtblk != null)
{
parent = VisualTreeHelper.GetParent(txtblk);
if (parent is not TextBlock)
{
parent = VisualTreeHelper.GetParent(parent);
if (parent is not TextBlock)
{
parent = VisualTreeHelper.GetParent(parent);
}
}
returnTxtblk = (TextBlock)parent;
}
return returnTxtblk;
}
void handleButtonPress(Point point, Ellipse ellipse)
{
//Delete ellipse before running inputhittest or it will return the ellipse
canvMain.Children.Remove(ellipse);
IInputElement element = InputHitTest(point);
object sender = element;
AddEllipseAt(canvMain, point, ellipse);
keyboardPress(sender);
}
private void Keyboard_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
sim.Keyboard.KeyUp(VirtualKeyCode.SHIFT);
sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
sim.Keyboard.KeyUp(VirtualKeyCode.MENU);
sim.Keyboard.KeyUp(VirtualKeyCode.LWIN);
if (Console.CapsLock) { sim.Keyboard.KeyPress(VirtualKeyCode.CAPITAL); }
dispatcherTimer.Stop();
//Make null so it can be called again
MainWindow.osk = null;
}
}
public class buttonEvents
{
public void RaiseEventRT()
{
pressRTEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressRTEvent;
public void RaiseEventLT()
{
pressLTEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressLTEvent;
public void RaiseEventA()
{
pressAEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressAEvent;
public event EventHandler pressXEvent;
public void RaiseEventX()
{
pressXEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressYEvent;
public void RaiseEventY()
{
pressYEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressBEvent;
public void RaiseEventB()
{
pressBEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressLBEvent;
public void RaiseEventLB()
{
pressLBEvent?.Invoke(this, EventArgs.Empty);
}
public event EventHandler pressRBEvent;
public void RaiseEventRB()
{
pressRBEvent?.Invoke(this, EventArgs.Empty);
}
}
}