1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Reflection;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Runtime.Serialization;
8
using System.Windows.Forms;
9
using Ear = SharpLib.Ear;
10
using Hid = SharpLib.Hid;
11
12
namespace SharpDisplayManager
13
{
14
[DataContract]
15
[Ear.AttributeObject(Id = "Event.Hid", Name = "HID", Description = "Handle input from Keyboards and Remotes.")]
16
public class EventHid: Ear.Event
17
18
public EventHid()
19
20
}
21
22
[DataMember]
23
public ushort UsagePage { get; set; }
24
25
26
public ushort UsageCollection { get; set; }
27
28
29
public ushort Usage { get; set; }
30
31
32
public Keys Key { get; set; }
33
34
35
[Ear.AttributeObjectProperty
36
(
37
Id = "HID.Keyboard.IsKeyUp",
38
Name = "Key Up",
39
Description = "Key up if set, key down otherwise."
40
)]
41
public bool IsKeyUp { get; set; } = false;
42
43
44
public bool IsMouse { get; set; } = false;
45
46
47
public bool IsKeyboard { get; set; } = false;
48
49
50
public bool IsGeneric { get; set; } = false;
51
52
53
public bool HasModifierShift { get; set; } = false;
54
55
56
public bool HasModifierControl { get; set; } = false;
57
58
59
public bool HasModifierAlt { get; set; } = false;
60
61
62
public bool HasModifierWindows { get; set; } = false;
63
64
65
public string UsageName { get; set; } = "Press a key";
66
67
68
69
protected override void DoConstruct()
70
71
base.DoConstruct();
72
UpdateDynamicProperties();
73
74
75
private void UpdateDynamicProperties()
76
77
78
79
80
/// <summary>
81
/// Make sure we distinguish between various configuration of this event
82
/// </summary>
83
/// <returns></returns>
84
public override string Brief()
85
86
string brief = Name + ": ";
87
88
if (!IsValid())
89
90
brief += "Press a key";
91
return brief;
92
93
94
if (IsKeyboard)
95
96
brief += Key.ToString();
97
98
if (HasModifierAlt)
99
100
brief += " + ALT";
101
102
103
if (HasModifierControl)
104
105
brief += " + CTRL";
106
107
108
if (HasModifierShift)
109
110
brief += " + SHIFT";
111
112
113
if (HasModifierWindows)
114
115
brief += " + WIN";
116
117
118
else if (IsGeneric)
119
120
brief += UsageName;
121
122
123
if (IsKeyUp)
124
125
brief += " (UP)";
126
127
else
128
129
brief += " (DOWN)";
130
131
132
133
134
135
136
///
137
138
/// <param name="obj"></param>
139
140
public override bool Equals(object obj)
141
142
if (obj is EventHid)
143
144
EventHid e = (EventHid)obj;
145
return e.Key == Key
146
&& e.Usage == Usage
147
&& e.UsagePage == UsagePage
148
&& e.UsageCollection == UsageCollection
149
&& e.IsKeyUp == IsKeyUp
150
&& e.IsGeneric == IsGeneric
151
&& e.IsKeyboard == IsKeyboard
152
&& e.IsMouse == IsMouse
153
&& e.HasModifierAlt == HasModifierAlt
154
&& e.HasModifierControl == HasModifierControl
155
&& e.HasModifierShift == HasModifierShift
156
&& e.HasModifierWindows == HasModifierWindows;
157
158
159
return false;
160
161
162
163
164
165
166
protected override void OnStateLeave()
167
168
if (CurrentState == State.Edit)
169
170
// Leaving edit mode
171
// Unhook HID events
172
Program.HidHandler.OnHidEvent -= HandleHidEvent;
173
174
175
176
177
178
179
180
protected override void OnStateEnter()
181
182
183
184
// Enter edit mode
185
// Hook-in HID events
186
Program.HidHandler.OnHidEvent += HandleHidEvent;
187
188
189
190
191
192
/// Here we receive HID events from our HID library.
193
194
/// <param name="aSender"></param>
195
/// <param name="aHidEvent"></param>
196
public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent)
197
198
if (CurrentState != State.Edit
199
|| aHidEvent.IsMouse
200
|| aHidEvent.IsButtonUp
201
|| !aHidEvent.IsValid
202
|| aHidEvent.IsBackground
203
|| aHidEvent.IsRepeat
204
|| aHidEvent.IsStray)
205
206
return;
207
208
209
PrivateCopy(aHidEvent);
210
//
211
212
//Tell observer the object itself changed
213
OnPropertyChanged("Brief");
214
215
216
217
218
219
220
public void Copy(Hid.Event aHidEvent)
221
222
223
//We need the key up/down too here
224
IsKeyUp = aHidEvent.IsButtonUp;
225
226
227
228
229
230
231
private void PrivateCopy(Hid.Event aHidEvent)
232
233
//Copy for scan
234
UsagePage = aHidEvent.UsagePage;
235
UsageCollection = aHidEvent.UsageCollection;
236
IsGeneric = aHidEvent.IsGeneric;
237
IsKeyboard = aHidEvent.IsKeyboard;
238
IsMouse = aHidEvent.IsMouse;
239
240
if (IsGeneric)
241
242
if (aHidEvent.Usages.Count > 0)
243
244
Usage = aHidEvent.Usages[0];
245
UsageName = aHidEvent.UsageName(0);
246
247
248
Key = Keys.None;
249
HasModifierAlt = false;
250
HasModifierControl = false;
251
HasModifierShift = false;
252
HasModifierWindows = false;
253
254
else if (IsKeyboard)
255
256
Usage = 0;
257
Key = aHidEvent.VirtualKey;
258
HasModifierAlt = aHidEvent.HasModifierAlt;
259
HasModifierControl = aHidEvent.HasModifierControl;
260
HasModifierShift = aHidEvent.HasModifierShift;
261
HasModifierWindows = aHidEvent.HasModifierWindows;
262
263
264
265
266
267
268
269
270
public override bool IsValid()
271
272
return IsGeneric || IsKeyboard;
273
274
275