aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs
blob: 0bbba03adedd4db60e39ba523b16472723c991bc (plain)
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
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 Forms = System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.Commands;
using System.Threading;
using System.Collections.ObjectModel;
using System.Media;
using Tango.Core.Helpers;
using Tango.Core.EventArguments;
using System.Windows.Controls.Primitives;
using Tango.Touch.Controls;
using System.Diagnostics;

namespace Tango.Touch.Keyboard
{
    public class TouchKeyboard : Control
    {
        private SoundPlayer _soundPlayer;
        private Thumb _jogging_thumb;
        private RadialGradientBrush _jogging_brush;

        //The system keyboard event.
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
        public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
        public const int VK_CONTROL = 0xA3; //Control key code
        private double _jog_start_value;
        private double _last_drag_delta;
        private IValueControl _jog_control;

        static TouchKeyboard()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchKeyboard), new FrameworkPropertyMetadata(typeof(TouchKeyboard)));
        }

        private bool _isInitialized;

        public event EventHandler<KeyboardActionKeyMode> ActionKeyPressed;
        public event EventHandler<String> KeyPressed;

        public bool IsSpecialCharactersOn
        {
            get { return (bool)GetValue(IsSpecialCharactersOnProperty); }
            set { SetValue(IsSpecialCharactersOnProperty, value); }
        }
        public static readonly DependencyProperty IsSpecialCharactersOnProperty =
            DependencyProperty.Register("IsSpecialCharactersOn", typeof(bool), typeof(TouchKeyboard), new PropertyMetadata(false, (d, e) => (d as TouchKeyboard).OnSpecialCharactersOnChanged()));

        public TouchKeyboardMode Mode
        {
            get { return (TouchKeyboardMode)GetValue(ModeProperty); }
            set { SetValue(ModeProperty, value); }
        }
        public static readonly DependencyProperty ModeProperty =
            DependencyProperty.Register("Mode", typeof(TouchKeyboardMode), typeof(TouchKeyboard), new PropertyMetadata(TouchKeyboardMode.AlphaNumeric));

        public KeyboardOutputMode OutputMode
        {
            get { return (KeyboardOutputMode)GetValue(OutputModeProperty); }
            set { SetValue(OutputModeProperty, value); }
        }
        public static readonly DependencyProperty OutputModeProperty =
            DependencyProperty.Register("OutputMode", typeof(KeyboardOutputMode), typeof(TouchKeyboard), new PropertyMetadata(KeyboardOutputMode.Wpf));

        public KeyboardActionKeyMode ActionKeyMode
        {
            get { return (KeyboardActionKeyMode)GetValue(ActionKeyModeProperty); }
            set { SetValue(ActionKeyModeProperty, value); }
        }
        public static readonly DependencyProperty ActionKeyModeProperty =
            DependencyProperty.Register("ActionKeyMode", typeof(KeyboardActionKeyMode), typeof(TouchKeyboard), new PropertyMetadata(KeyboardActionKeyMode.Next, (d, e) => (d as TouchKeyboard).OnActionKeyModeChanged()));

        public String ActionKeyText
        {
            get { return (String)GetValue(ActionKeyTextProperty); }
            private set { SetValue(ActionKeyTextProperty, value); }
        }
        public static readonly DependencyProperty ActionKeyTextProperty =
            DependencyProperty.Register("ActionKeyText", typeof(String), typeof(TouchKeyboard), new PropertyMetadata(null));

        public KeyboardDefinition CurrentKeyboardDefinition
        {
            get { return (KeyboardDefinition)GetValue(CurrentKeyboardDefinitionProperty); }
            set { SetValue(CurrentKeyboardDefinitionProperty, value); }
        }
        public static readonly DependencyProperty CurrentKeyboardDefinitionProperty =
            DependencyProperty.Register("CurrentKeyboardDefinition", typeof(KeyboardDefinition), typeof(TouchKeyboard), new PropertyMetadata(null));

        public int CurrentKeyboardDefinitionIndex
        {
            get { return (int)GetValue(CurrentKeyboardDefinitionIndexProperty); }
            set { SetValue(CurrentKeyboardDefinitionIndexProperty, value); }
        }
        public static readonly DependencyProperty CurrentKeyboardDefinitionIndexProperty =
            DependencyProperty.Register("CurrentKeyboardDefinitionIndex", typeof(int), typeof(TouchKeyboard), new PropertyMetadata(0));

        public ObservableCollection<KeyboardDefinition> KeyboardDefinitions
        {
            get { return (ObservableCollection<KeyboardDefinition>)GetValue(KeyboardDefinitionsProperty); }
            set { SetValue(KeyboardDefinitionsProperty, value); }
        }
        public static readonly DependencyProperty KeyboardDefinitionsProperty =
            DependencyProperty.Register("KeyboardDefinitions", typeof(ObservableCollection<KeyboardDefinition>), typeof(TouchKeyboard), new PropertyMetadata(null));

        public Size KeySize
        {
            get { return (Size)GetValue(KeySizeProperty); }
            set { SetValue(KeySizeProperty, value); }
        }
        public static readonly DependencyProperty KeySizeProperty =
            DependencyProperty.Register("KeySize", typeof(Size), typeof(TouchKeyboard), new PropertyMetadata(Size.Empty));

        public double MaxSpecialKeyWidth
        {
            get { return (double)GetValue(MaxSpecialKeyWidthProperty); }
            set { SetValue(MaxSpecialKeyWidthProperty, value); }
        }
        public static readonly DependencyProperty MaxSpecialKeyWidthProperty =
            DependencyProperty.Register("MaxSpecialKeyWidth", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(0.0));

        public KeysLineDefinition NumbersLineDefinition
        {
            get { return (KeysLineDefinition)GetValue(NumbersLineDefinitionProperty); }
            set { SetValue(NumbersLineDefinitionProperty, value); }
        }
        public static readonly DependencyProperty NumbersLineDefinitionProperty =
            DependencyProperty.Register("NumbersLineDefinition", typeof(KeysLineDefinition), typeof(TouchKeyboard), new PropertyMetadata(null));

        public Brush CharactersBackground
        {
            get { return (Brush)GetValue(CharactersBackgroundProperty); }
            set { SetValue(CharactersBackgroundProperty, value); }
        }
        public static readonly DependencyProperty CharactersBackgroundProperty =
            DependencyProperty.Register("CharactersBackground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(Brushes.Gainsboro));

        public Brush CharactersForeground
        {
            get { return (Brush)GetValue(CharactersForegroundProperty); }
            set { SetValue(CharactersForegroundProperty, value); }
        }
        public static readonly DependencyProperty CharactersForegroundProperty =
            DependencyProperty.Register("CharactersForeground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(20, 20, 20))));

        public Brush SpecialCharactersBackground
        {
            get { return (Brush)GetValue(SpecialCharactersBackgroundProperty); }
            set { SetValue(SpecialCharactersBackgroundProperty, value); }
        }
        public static readonly DependencyProperty SpecialCharactersBackgroundProperty =
            DependencyProperty.Register("SpecialCharactersBackground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(Brushes.Gray));

        public Brush SpecialCharactersForeground
        {
            get { return (Brush)GetValue(SpecialCharactersForegroundProperty); }
            set { SetValue(SpecialCharactersForegroundProperty, value); }
        }
        public static readonly DependencyProperty SpecialCharactersForegroundProperty =
            DependencyProperty.Register("SpecialCharactersForeground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(Brushes.Gainsboro));

        public double VectorMarkupSize
        {
            get { return (double)GetValue(VectorMarkupSizeProperty); }
            set { SetValue(VectorMarkupSizeProperty, value); }
        }
        public static readonly DependencyProperty VectorMarkupSizeProperty =
            DependencyProperty.Register("VectorMarkupSize", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(10.0));

        public double DotsSize
        {
            get { return (double)GetValue(DotsSizeProperty); }
            set { SetValue(DotsSizeProperty, value); }
        }
        public static readonly DependencyProperty DotsSizeProperty =
            DependencyProperty.Register("DotsSize", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(5.0));

        public double CharactersCornerRadius
        {
            get { return (double)GetValue(CharactersCornerRadiusProperty); }
            set { SetValue(CharactersCornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CharactersCornerRadiusProperty =
            DependencyProperty.Register("CharactersCornerRadius", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(5.0));

        public CapsLockMode CapsLockMode
        {
            get { return (CapsLockMode)GetValue(CapsLockModeProperty); }
            set { SetValue(CapsLockModeProperty, value); }
        }
        public static readonly DependencyProperty CapsLockModeProperty =
            DependencyProperty.Register("CapsLockMode", typeof(CapsLockMode), typeof(TouchKeyboard), new PropertyMetadata(CapsLockMode.None));

        public bool IsCapsLockOn
        {
            get { return (bool)GetValue(IsCapsLockOnProperty); }
            private set { SetValue(IsCapsLockOnProperty, value); }
        }
        public static readonly DependencyProperty IsCapsLockOnProperty =
            DependencyProperty.Register("IsCapsLockOn", typeof(bool), typeof(TouchKeyboard), new PropertyMetadata(false));

        public RelayCommand<KeyDefinition> KeyDefinitionCommand
        {
            get { return (RelayCommand<KeyDefinition>)GetValue(KeyDefinitionCommandProperty); }
            set { SetValue(KeyDefinitionCommandProperty, value); }
        }
        public static readonly DependencyProperty KeyDefinitionCommandProperty =
            DependencyProperty.Register("KeyDefinitionCommand", typeof(RelayCommand<KeyDefinition>), typeof(TouchKeyboard), new PropertyMetadata(null));

        public RelayCommand<SpecialKeyDefinition> SpecialKeyDefinitionCommand
        {
            get { return (RelayCommand<SpecialKeyDefinition>)GetValue(SpecialKeyDefinitionCommandProperty); }
            set { SetValue(SpecialKeyDefinitionCommandProperty, value); }
        }
        public static readonly DependencyProperty SpecialKeyDefinitionCommandProperty =
            DependencyProperty.Register("SpecialKeyDefinitionCommand", typeof(RelayCommand<SpecialKeyDefinition>), typeof(TouchKeyboard), new PropertyMetadata(null));

        public RelayCommand SpecialCharactersCommand
        {
            get { return (RelayCommand)GetValue(SpecialCharactersCommandProperty); }
            set { SetValue(SpecialCharactersCommandProperty, value); }
        }
        public static readonly DependencyProperty SpecialCharactersCommandProperty =
            DependencyProperty.Register("SpecialCharactersCommand", typeof(RelayCommand), typeof(TouchKeyboard), new PropertyMetadata(null));

        public RelayCommand<String> FreeTextCommand
        {
            get { return (RelayCommand<String>)GetValue(FreeTextCommandProperty); }
            set { SetValue(FreeTextCommandProperty, value); }
        }
        public static readonly DependencyProperty FreeTextCommandProperty =
            DependencyProperty.Register("FreeTextCommand", typeof(RelayCommand<String>), typeof(TouchKeyboard), new PropertyMetadata(null));

        public RelayCommand NextLanguageCommand
        {
            get { return (RelayCommand)GetValue(NextLanguageCommandProperty); }
            set { SetValue(NextLanguageCommandProperty, value); }
        }
        public static readonly DependencyProperty NextLanguageCommandProperty =
            DependencyProperty.Register("NextLanguageCommand", typeof(RelayCommand), typeof(TouchKeyboard), new PropertyMetadata(null));

        public RelayCommand ActionKeyCommand
        {
            get { return (RelayCommand)GetValue(ActionKeyCommandProperty); }
            set { SetValue(ActionKeyCommandProperty, value); }
        }
        public static readonly DependencyProperty ActionKeyCommandProperty =
            DependencyProperty.Register("ActionKeyCommand", typeof(RelayCommand), typeof(TouchKeyboard), new PropertyMetadata(null));

        public RelayCommand<Object> NumericKeyCommand
        {
            get { return (RelayCommand<Object>)GetValue(NumericKeyCommandProperty); }
            set { SetValue(NumericKeyCommandProperty, value); }
        }
        public static readonly DependencyProperty NumericKeyCommandProperty =
            DependencyProperty.Register("NumericKeyCommand", typeof(RelayCommand<Object>), typeof(TouchKeyboard), new PropertyMetadata(null));


        public TouchKeyboard()
        {
            KeyboardDefinitions = new ObservableCollection<KeyboardDefinition>();
            Initialize();

            this.SizeChanged += (_, __) => SetKeySize();
            KeyDefinitionCommand = new RelayCommand<KeyDefinition>(InvokeKeyDefinition);
            SpecialKeyDefinitionCommand = new RelayCommand<SpecialKeyDefinition>(InvokeSpecialKeyDefinition);
            SpecialCharactersCommand = new RelayCommand(ToggleSpecialKeys);
            FreeTextCommand = new RelayCommand<string>((x) => SendKeys(x));
            NextLanguageCommand = new RelayCommand(NextLanguage);
            ActionKeyCommand = new RelayCommand(InvokeActionKey);
            NumericKeyCommand = new RelayCommand<object>((x) => SendKeys(x.ToString().Replace("'", "")));

            _soundPlayer = new SoundPlayer(EmbeddedResourceHelper.GetEmbeddedResourceStream("Tango.Touch.Keyboard.keyboard-iphone.wav"));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _jogging_thumb = GetTemplateChild("PART_JoggingThumb") as Thumb;
            _jogging_brush = GetTemplateChild("PART_Jog_Gradient") as RadialGradientBrush;


            _jogging_thumb.DragStarted += _jogging_thumb_DragStarted;
            _jogging_thumb.DragCompleted += _jogging_thumb_DragCompleted;
            _jogging_thumb.DragDelta += _jogging_thumb_DragDelta;

            RepeatButton btnUp = GetTemplateChild("PART_JogUp") as RepeatButton;
            RepeatButton btnDown = GetTemplateChild("PART_JogDown") as RepeatButton;

            btnUp.Click += BtnUp_Click;
            btnDown.Click += BtnDown_Click;
        }

        private void BtnDown_Click(object sender, RoutedEventArgs e)
        {
            IInputElement focusedControl = FocusManager.GetFocusedElement(Application.Current.MainWindow);

            if (focusedControl != null)
            {
                IValueControl control = (focusedControl as DependencyObject).FindAncestor<IValueControl>();

                if (control != null)
                {
                    if (control.Step > 0)
                    {
                        control.Value -= control.Step;
                    }
                    else
                    {
                        control.Value -= 1;
                    }
                }
            }
        }

        private void BtnUp_Click(object sender, RoutedEventArgs e)
        {
            IInputElement focusedControl = FocusManager.GetFocusedElement(Application.Current.MainWindow);

            if (focusedControl != null)
            {
                IValueControl control = (focusedControl as DependencyObject).FindAncestor<IValueControl>();

                if (control != null)
                {
                    if (control.Step > 0)
                    {
                        control.Value += control.Step;
                    }
                    else
                    {
                        control.Value += 1;
                    }
                }
            }
        }

        private void _jogging_thumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            IInputElement focusedControl = FocusManager.GetFocusedElement(Application.Current.MainWindow);

            if (focusedControl != null)
            {
                IValueControl control = (focusedControl as DependencyObject).FindAncestor<IValueControl>();

                if (control != null)
                {
                    _jog_control = control;
                    _jog_start_value = _jog_control.Value;
                }
            }
        }

        private void _jogging_thumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            _jogging_brush.GradientOrigin = new Point(0.5, 0.5);
            _jog_start_value = 0;
            _jog_control = null;
        }

        private void _jogging_thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (_jog_control != null)
            {
                double min_origin = 0.4;
                double max_origin = 0.6;

                var factor = _jog_control.Maximum / Application.Current.MainWindow.Height;

                if (_jog_control.AutoCalculateJogStep)
                {
                    _jog_control.Value = _jog_start_value + (((-e.VerticalChange) * factor) * _jog_control.JoggingFactor);
                }
                else
                {
                    _jog_control.Value = _jog_start_value + (-e.VerticalChange * _jog_control.JoggingFactor);
                }

                if (_last_drag_delta > e.VerticalChange)
                {
                    _jogging_brush.GradientOrigin = new Point(0.5, _jogging_brush.GradientOrigin.Y - 0.005);

                    if (_jogging_brush.GradientOrigin.Y < min_origin)
                    {
                        _jogging_brush.GradientOrigin = new Point(0.5, min_origin);
                    }
                }
                else if (_last_drag_delta < e.VerticalChange)
                {
                    _jogging_brush.GradientOrigin = new Point(0.5, _jogging_brush.GradientOrigin.Y + 0.005);

                    if (_jogging_brush.GradientOrigin.Y > max_origin)
                    {
                        _jogging_brush.GradientOrigin = new Point(0.5, max_origin);
                    }
                }
            }

            _last_drag_delta = e.VerticalChange;
        }

        private void Initialize()
        {
            if (!_isInitialized)
            {
                var numbers = new KeysLineDefinition();

                for (int i = 1; i < 10; i++)
                {
                    numbers.KeyDefinitions.Add(new KeyDefinition()
                    {
                        StandardText = i.ToString(),
                        CapsText = i.ToString(),
                    });
                }

                numbers.KeyDefinitions.Add(new KeyDefinition()
                {
                    StandardText = "0",
                    CapsText = "0",
                });

                NumbersLineDefinition = numbers;

                CurrentKeyboardDefinition = KeyboardDefinition.Default;
                KeyboardDefinitions.Add(CurrentKeyboardDefinition);
                //KeyboardDefinitions.Add(KeyboardDefinition.CreateDefaultHebrewAlphaNumeric());

                SetKeySize();

                OnActionKeyModeChanged();

                _isInitialized = true;
            }
        }

        private void SetKeySize()
        {
            KeySize = new Size(ActualWidth / NumbersLineDefinition.KeyDefinitions.Count, (ActualWidth / NumbersLineDefinition.KeyDefinitions.Count) * 0.90);
            MaxSpecialKeyWidth = KeySize.Width * 2;
        }

        private void InvokeKeyDefinition(KeyDefinition key)
        {
            if (IsCapsLockOn)
            {
                SendKeys(key.CapsText);
            }
            else
            {
                //UIAutomationHelper.GetActiveWindow();

                //IInputElement focusedControl = FocusManager.GetFocusedElement(this);

                SendKeys(key.StandardText);
            }
        }

        private void InvokeSpecialKeyDefinition(SpecialKeyDefinition key)
        {
            switch (key.Type)
            {
                case SpecialKeyType.StandardText:
                    SendKeys(key.Output);
                    break;
                case SpecialKeyType.CapsLock:

                    if (CapsLockMode == CapsLockMode.None)
                    {
                        CapsLockMode = CapsLockMode.SingleChar;
                    }
                    else if (CapsLockMode == CapsLockMode.SingleChar)
                    {
                        CapsLockMode = CapsLockMode.Locked;
                    }
                    else
                    {
                        CapsLockMode = CapsLockMode.None;
                    }

                    IsCapsLockOn = CapsLockMode != CapsLockMode.None;

                    break;
            }
        }

        private void SendKeys(String key)
        {
            //Application.Current.MainWindow.Activate();

            var isSpecialChar = IsSpecialCharactersOn;
            var definition = CurrentKeyboardDefinition;

            if (CapsLockMode == CapsLockMode.SingleChar)
            {
                CapsLockMode = CapsLockMode.None;
                IsCapsLockOn = false;
            }


            if (key.Length > 1 || (isSpecialChar && definition.KeysLinesDefinitions.SelectMany(x => x.KeyDefinitions).Select(x => x.StandardText).Contains(key)) || OutputMode == KeyboardOutputMode.Windows)
            {
                Core.Threading.ThreadFactory.StartNew(() =>
                {
                    if (key.Contains("{"))
                    {
                        key = key.Replace("'","");
                        Forms.SendKeys.SendWait(key);
                    }
                    else if (key != " ")
                    {
                        Forms.SendKeys.SendWait("{" + key + "}");
                    }
                    else
                    {
                        Forms.SendKeys.SendWait(" ");
                    }

                    Forms.SendKeys.Flush();
                });
            }
            else
            {
                //Forms.SendKeys.SendWait(key);

                var text = key;
                var target = FocusManager.GetFocusedElement(Application.Current.MainWindow) as UIElement;

                Key k = Key.None;
                Enum.TryParse<Key>(key, out k);

                RaiseKeyUp(target, k);

                var routedEvent = TextCompositionManager.TextInputEvent;

                target.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.PrimaryKeyboardDevice,
                    new TextComposition(InputManager.Current, target, text))
                {
                    RoutedEvent = routedEvent
                }
                );
            }

            _soundPlayer.Play();
        }

        private void RaiseKeyDown(UIElement target, Key key)
        {
            target.RaiseEvent(
  new KeyEventArgs(
    System.Windows.Input.Keyboard.PrimaryDevice,
    PresentationSource.FromVisual(target),
    0,
    key)
  { RoutedEvent = System.Windows.Input.Keyboard.PreviewKeyDownEvent });

            target.RaiseEvent(
              new KeyEventArgs(
                System.Windows.Input.Keyboard.PrimaryDevice,
                PresentationSource.FromVisual(target),
                0,
                key)
              { RoutedEvent = System.Windows.Input.Keyboard.KeyDownEvent });

        }

        private void RaiseKeyUp(UIElement target, Key key)
        {
            target.RaiseEvent(
new KeyEventArgs(
System.Windows.Input.Keyboard.PrimaryDevice,
PresentationSource.FromVisual(target),
0,
key)
{ RoutedEvent = System.Windows.Input.Keyboard.PreviewKeyUpEvent });

            target.RaiseEvent(
              new KeyEventArgs(
                System.Windows.Input.Keyboard.PrimaryDevice,
                PresentationSource.FromVisual(target),
                0,
                key)
              { RoutedEvent = System.Windows.Input.Keyboard.KeyUpEvent }
            );
        }

        public static void PressCtrl()
        {
            keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
        }

        public static void ReleaseCtrl()
        {
            keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
        }

        private void OnSpecialCharactersOnChanged()
        {
            if (!IsSpecialCharactersOn)
            {
                CurrentKeyboardDefinition = KeyboardDefinitions[CurrentKeyboardDefinitionIndex];
            }
            else
            {
                KeyboardDefinition definition = new KeyboardDefinition();
                definition.EnterText = CurrentKeyboardDefinition.EnterText;
                definition.KeysLinesDefinitions = KeyboardDefinition.CreateDefaultSpecialCharacters();
                CurrentKeyboardDefinition = definition;
            }
        }

        private void ToggleSpecialKeys()
        {
            IsSpecialCharactersOn = !IsSpecialCharactersOn;
        }

        private void NextLanguage()
        {
            IsCapsLockOn = false;

            CurrentKeyboardDefinitionIndex++;

            if (CurrentKeyboardDefinitionIndex > KeyboardDefinitions.Count - 1)
            {
                CurrentKeyboardDefinitionIndex = 0;
            }

            CurrentKeyboardDefinition = KeyboardDefinitions[CurrentKeyboardDefinitionIndex];

            OnActionKeyModeChanged();
        }

        private void OnActionKeyModeChanged()
        {
            switch (ActionKeyMode)
            {
                case KeyboardActionKeyMode.Go:
                    ActionKeyText = CurrentKeyboardDefinition.EnterText;
                    break;
                case KeyboardActionKeyMode.Return:
                    ActionKeyText = "↵";
                    break;
                case KeyboardActionKeyMode.Next:
                    ActionKeyText = CurrentKeyboardDefinition.TabText;
                    break;
                case KeyboardActionKeyMode.Search:
                    ActionKeyText = "Search"; // TODO: Make trigger in view and vector path.
                    break;
            }
        }

        private void InvokeActionKey()
        {
            switch (ActionKeyMode)
            {
                case KeyboardActionKeyMode.Search:
                case KeyboardActionKeyMode.Return:
                case KeyboardActionKeyMode.Go:
                    SendKeys("{ENTER}");
                    break;
                case KeyboardActionKeyMode.Next:
                    SendKeys("{TAB}");
                    break;
            }

            ActionKeyPressed?.Invoke(this, ActionKeyMode);
        }
    }
}