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
|
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;
namespace Tango.Touch.Keyboard
{
public class TouchKeyboard : Control
{
private SoundPlayer _soundPlayer;
//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
static TouchKeyboard()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchKeyboard), new FrameworkPropertyMetadata(typeof(TouchKeyboard)));
}
private bool _isInitialized;
public event EventHandler<KeyboardActionKeyMode> ActionKeyPressed;
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 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"));
}
private void Initialize()
{
if (!_isInitialized)
{
var numbers = new KeysLineDefinition();
for (int i = 1; i < 10; i++)
{
numbers.KeyDefinitions.Add(new KeyDefinition()
{
StandardText = i.ToString(),
});
}
numbers.KeyDefinitions.Add(new KeyDefinition()
{
StandardText = "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 Task SendKeys(String key)
{
Application.Current.MainWindow.Activate();
var isSpecialChar = IsSpecialCharactersOn;
var definition = CurrentKeyboardDefinition;
if (CapsLockMode == CapsLockMode.SingleChar)
{
CapsLockMode = CapsLockMode.None;
IsCapsLockOn = false;
}
return Task.Factory.StartNew(() =>
{
_soundPlayer.Play();
if (isSpecialChar && definition.KeysLinesDefinitions.SelectMany(x => x.KeyDefinitions).Select(x => x.StandardText).Contains(key))
{
Forms.SendKeys.SendWait("{" + key + "}");
}
else
{
Forms.SendKeys.SendWait(key);
}
});
}
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.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.Go:
SendKeys("{ENTER}");
break;
case KeyboardActionKeyMode.Next:
SendKeys("{TAB}");
break;
case KeyboardActionKeyMode.Search:
SendKeys("{ENTER}");
break;
}
ActionKeyPressed?.Invoke(this, ActionKeyMode);
}
}
}
|