MartixKeyboardInputProvider.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
#define MF3

using System;
using System.Collections;
using System.Windows.Input;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Hardware;
using UAM.InformatiX.SPOT.Hardware;

namespace UAM.InformatiX.SPOT.Input
{
public sealed class MatrixKeyboardInputProvider
{
private class ModifierTuplet
{
public MatrixKeyboard.KeyLocation Location;
public ModifierKeys Modifier;
public ModifierBehavior Behavior;
}
private class ButtonTuplet
{
public MatrixKeyboard.KeyLocation Location;
public ModifierKeys Modifier;
public Button Button;
}

public readonly Dispatcher Dispatcher;

private MatrixKeyboard _keyboard;
private ReportInputCallback _reportCallback;
private RaiseEventCallback _eventCallback;
private InputProviderSite _site;
private PresentationSource _source;

public MatrixKeyboardInputProvider(MatrixKeyboard keyboard, PresentationSource source)
{
_source = source;
_keyboard = keyboard;
_site = InputManager.CurrentInputManager.RegisterInputProvider(this);
_reportCallback = new ReportInputCallback(_site.ReportInput);
_eventCallback = new RaiseEventCallback(RaiseEvent);
Dispatcher = Dispatcher.CurrentDispatcher;

_keyboard.KeyPressed += new MatrixKeyboard.KeyEventHandler(KeyPressed);
_keyboard.KeyReleased += new MatrixKeyboard.KeyEventHandler(KeyReleased);
}

private ArrayList _currentModifiers = new ArrayList();
private ModifierKeys _currentModifier = ModifierKeys.None;
public ModifierKeys CurrentModifier { get { return _currentModifier; } }

private ModifierKeys GetCurrentModifier()
{
ModifierKeys modifier = ModifierKeys.None;
for (int i = 0; i < _currentModifiers.Count; i++)
modifier |= ((ModifierTuplet)_currentModifiers[i]).Modifier;

return modifier;
}
private void RemoveTemporaryModifiers()
{
ModifierKeys modifier = ModifierKeys.None;
for (int i = _currentModifiers.Count - 1; i >= 0; i--)
{
ModifierTuplet t = (ModifierTuplet)_currentModifiers[i];
switch (t.Behavior)
{
case ModifierBehavior.StickyKey:
_currentModifiers.RemoveAt(i);
break;
case ModifierBehavior.ToggleKey:
case ModifierBehavior.Normal:
default:
modifier |= t.Modifier;
break;
}
}

if (_currentModifier != modifier)
{
_currentModifier = modifier;
OnCurrentModifierChanged();
}
}
private void AddCurrentModifier(ModifierTuplet tuplet)
{
_currentModifiers.Add(tuplet);

ModifierKeys newModifier = _currentModifier | tuplet.Modifier;
if (newModifier != _currentModifier)
{
_currentModifier = newModifier;
OnCurrentModifierChanged();
}
}
private void RemoveCurrentModifierAt(int index)
{
_currentModifiers.RemoveAt(index);

ModifierKeys newModifier = GetCurrentModifier();
if (newModifier != _currentModifier)
{
_currentModifier = newModifier;
OnCurrentModifierChanged();
}
}

private void KeyReleased(MatrixKeyboard keyboard, MatrixKeyboard.KeyLocation location, TimeSpan time)
{
ButtonTuplet b = GetButton(location, _currentModifier);
if (b != null)
{
ReportButton(b.Button, time, false);
RemoveTemporaryModifiers();
}
}
private void KeyPressed(MatrixKeyboard keyboard, MatrixKeyboard.KeyLocation location, TimeSpan time)
{
ModifierTuplet m = GetModifier(location);
if (m != null)
{
switch (m.Behavior)
{
case ModifierBehavior.Normal:
AddCurrentModifier(m);
break;
case ModifierBehavior.StickyKey:
case ModifierBehavior.ToggleKey:
int index = _currentModifiers.IndexOf(m);
if (index < 0)
AddCurrentModifier(m);
else
RemoveCurrentModifierAt(index);
break;
default:
throw new NotSupportedException("Unknown ModifierBehavior value.");
}
}

ButtonTuplet b = GetButton(location, _currentModifier);
if (b != null)
ReportButton(b.Button, time, true);
}

private delegate bool ReportInputCallback(InputReport inputReport);
private delegate void RaiseEventCallback(RoutedEventArgs args);
private void ReportButton(Button button, TimeSpan time, bool state)
{
RawButtonActions action = state ? RawButtonActions.ButtonDown : RawButtonActions.ButtonUp;
RawButtonInputReport report = new RawButtonInputReport(_source, time, button, action);

#if MF3
Dispatcher.BeginInvoke(_reportCallback, report);
#else
if
(button > Button.None && button <= Button.Last)
Dispatcher.BeginInvoke(_reportCallback, report);
else
{
RoutedEventArgs args = new UncheckedButtonEventArgs(null, time, button);
args.RoutedEvent = state ? Buttons.ButtonDownEvent : Buttons.ButtonUpEvent;
Dispatcher.BeginInvoke(_eventCallback, args);
}
#endif
}
private void RaiseEvent(RoutedEventArgs args)
{
UIElement target = Buttons.FocusedElement;
if (target != null)
target.RaiseEvent(args);
}

private ModifierTuplet[] _registeredModifiers = new ModifierTuplet[0];
private ModifierTuplet GetModifier(MatrixKeyboard.KeyLocation location)
{
for (int i = 0; i < _registeredModifiers.Length; i++)
if (_registeredModifiers[i].Location == location) return _registeredModifiers[i];

return null;
}
public void RegisterModifier(MatrixKeyboard.KeyLocation[] locations, ModifierKeys[] modifiers, ModifierBehavior[] behaviors)
{
if (locations == null) throw new ArgumentNullException("locations");
if (modifiers == null) throw new ArgumentNullException("modifiers");
if (behaviors == null) behaviors = new ModifierBehavior[locations.Length];

if (locations.Length != modifiers.Length || locations.Length != behaviors.Length) throw new ArgumentException("All arrays must be of the same length.");

ModifierTuplet[] tuplets = new ModifierTuplet[locations.Length];
for (int i = 0; i < locations.Length; i++)
{
if (modifiers[i] == ModifierKeys.None) throw new ArgumentException("'ModifierKeys.None' is not a valid modifier for registration.", "modifiers");
if (GetModifier(locations[i]) != null) throw new ArgumentException(locations[i] + " is already registered as a modifier.", "keys");

tuplets[i] = new ModifierTuplet();
tuplets[i].Location = locations[i];
tuplets[i].Behavior = behaviors[i];
tuplets[i].Modifier = modifiers[i];
}

int length = _registeredModifiers.Length;
ModifierTuplet[] newArrayT = new ModifierTuplet[length + tuplets.Length];

_registeredModifiers.CopyTo(newArrayT, 0);
tuplets.CopyTo(newArrayT, length);

lock (_registeredModifiers)
_registeredModifiers = newArrayT;
}
public void RegisterModifier(MatrixKeyboard.KeyLocation location, ModifierKeys modifier, ModifierBehavior behavior)
{
RegisterModifier(new MatrixKeyboard.KeyLocation[] { location }, new ModifierKeys[] { modifier }, new ModifierBehavior[] { behavior });
}

private ButtonTuplet[] _registeredButtons = new ButtonTuplet[0];
private ButtonTuplet GetButton(MatrixKeyboard.KeyLocation location, ModifierKeys modifier)
{
for (int i = 0; i < _registeredButtons.Length; i++)
if (_registeredButtons[i].Location == location && _registeredButtons[i].Modifier == modifier) return _registeredButtons[i];

return null;
}
public void RegisterButton(MatrixKeyboard.KeyLocation[] locations, ModifierKeys[] modifiers, Button[] buttons)
{
if (locations == null) throw new ArgumentNullException("locations");
if (modifiers == null) modifiers = new ModifierKeys[locations.Length];
if (buttons == null) throw new ArgumentNullException("buttons");

if (locations.Length != modifiers.Length || locations.Length != buttons.Length) throw new ArgumentException("All arrays must be of the same length.");

ButtonTuplet[] tuplets = new ButtonTuplet[locations.Length];
for (int i = 0; i < locations.Length; i++)
{
if (buttons[i] == Button.None) throw new ArgumentException("'Button.None' is not a valid button for registration.", "buttons");
if (GetButton(locations[i], modifiers[i]) != null) throw new ArgumentException(locations[i] + " with supplied modifier is already registered as a button.", "buttons");

tuplets[i] = new ButtonTuplet();
tuplets[i].Location = locations[i];
tuplets[i].Modifier = modifiers[i];
tuplets[i].Button = buttons[i];
}

int length = _registeredButtons.Length;
ButtonTuplet[] newArrayT = new ButtonTuplet[length + tuplets.Length];

_registeredButtons.CopyTo(newArrayT, 0);
tuplets.CopyTo(newArrayT, length);

lock (_registeredButtons)
_registeredButtons = newArrayT;
}
public void RegisterButton(MatrixKeyboard.KeyLocation location, ModifierKeys modifier, Button button)
{
RegisterButton(new MatrixKeyboard.KeyLocation[] { location }, new ModifierKeys[] { modifier }, new Button[] { button });
}
public void RegisterButton(MatrixKeyboard.KeyLocation[] locations, ModifierKeys modifier, Button[] buttons)
{
if (locations == null) throw new ArgumentNullException("locations");

ModifierKeys[] modifiers = new ModifierKeys[locations.Length];
if (modifier != ModifierKeys.None)
for (int i = 0; i < modifiers.Length; i++)
modifiers[i] = modifier;

RegisterButton(locations, modifiers, buttons);
}

private void OnCurrentModifierChanged()
{
if (CurrentModifierChanged != null)
CurrentModifierChanged(this, EventArgs.Empty);
}
public event EventHandler CurrentModifierChanged;
}
}