2 using System.Collections.Generic;
4 using System.Reflection;
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;
12 namespace SharpDisplayManager
15 [Ear.AttributeObject(Id = "Event.Hid", Name = "HID", Description = "Handle input from Keyboards and Remotes.")]
16 public class EventHid: Ear.Event
23 public ushort UsagePage { get; set; }
26 public ushort UsageCollection { get; set; }
29 public ushort Usage { get; set; }
32 public Keys Key { get; set; }
35 [Ear.AttributeObjectProperty
37 Id = "HID.Keyboard.IsKeyUp",
39 Description = "Key up if set, key down otherwise."
41 public bool IsKeyUp { get; set; } = false;
44 public bool IsMouse { get; set; } = false;
47 public bool IsKeyboard { get; set; } = false;
50 public bool IsGeneric { get; set; } = false;
53 public bool HasModifierShift { get; set; } = false;
56 public bool HasModifierControl { get; set; } = false;
59 public bool HasModifierAlt { get; set; } = false;
62 public bool HasModifierWindows { get; set; } = false;
65 public string UsageName { get; set; } = "Press a key";
69 protected override void DoConstruct()
72 UpdateDynamicProperties();
75 private void UpdateDynamicProperties()
81 /// Make sure we distinguish between various configuration of this event
83 /// <returns></returns>
84 public override string Brief()
86 string brief = AttributeName + ": ";
90 brief += "Press a key";
96 brief += Key.ToString();
103 if (HasModifierControl)
108 if (HasModifierShift)
113 if (HasModifierWindows)
138 /// <param name="obj"></param>
139 /// <returns></returns>
140 public override bool Equals(object obj)
144 EventHid e = (EventHid)obj;
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;
166 protected override void OnStateLeave()
168 if (CurrentState == State.Edit)
172 Program.HidHandler.OnHidEvent -= HandleHidEvent;
180 protected override void OnStateEnter()
182 if (CurrentState == State.Edit)
185 // Hook-in HID events
186 Program.HidHandler.OnHidEvent += HandleHidEvent;
192 /// Here we receive HID events from our HID library.
194 /// <param name="aSender"></param>
195 /// <param name="aHidEvent"></param>
196 public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent)
198 if (CurrentState != State.Edit
200 || aHidEvent.IsButtonUp
201 || !aHidEvent.IsValid
202 || aHidEvent.IsBackground
203 || aHidEvent.IsRepeat
204 || aHidEvent.IsStray)
209 PrivateCopy(aHidEvent);
212 //Tell observer the object itself changed
213 OnPropertyChanged("Brief");
219 /// <param name="aHidEvent"></param>
220 public void Copy(Hid.Event aHidEvent)
222 PrivateCopy(aHidEvent);
223 //We need the key up/down too here
224 IsKeyUp = aHidEvent.IsButtonUp;
230 /// <param name="aHidEvent"></param>
231 private void PrivateCopy(Hid.Event aHidEvent)
234 UsagePage = aHidEvent.UsagePage;
235 UsageCollection = aHidEvent.UsageCollection;
236 IsGeneric = aHidEvent.IsGeneric;
237 IsKeyboard = aHidEvent.IsKeyboard;
238 IsMouse = aHidEvent.IsMouse;
242 if (aHidEvent.Usages.Count > 0)
244 Usage = aHidEvent.Usages[0];
245 UsageName = aHidEvent.UsageName(0);
249 HasModifierAlt = false;
250 HasModifierControl = false;
251 HasModifierShift = false;
252 HasModifierWindows = false;
257 Key = aHidEvent.VirtualKey;
258 HasModifierAlt = aHidEvent.HasModifierAlt;
259 HasModifierControl = aHidEvent.HasModifierControl;
260 HasModifierShift = aHidEvent.HasModifierShift;
261 HasModifierWindows = aHidEvent.HasModifierWindows;
269 /// <returns></returns>
270 public override bool IsValid()
272 return IsGeneric || IsKeyboard;