StephaneLenclud@244: using System; StephaneLenclud@244: using System.Collections.Generic; StephaneLenclud@244: using System.Linq; StephaneLenclud@244: using System.Reflection; StephaneLenclud@244: using System.Text; StephaneLenclud@244: using System.Threading.Tasks; StephaneLenclud@244: using System.Runtime.Serialization; StephaneLenclud@244: using System.Windows.Forms; StephaneLenclud@244: using Ear = SharpLib.Ear; StephaneLenclud@244: using Hid = SharpLib.Hid; StephaneLenclud@244: StephaneLenclud@244: namespace SharpDisplayManager StephaneLenclud@244: { StephaneLenclud@244: [DataContract] StephaneLenclud@247: [Ear.AttributeObject(Id = "Event.Hid", Name = "HID", Description = "Handle input from Keyboards and Remotes.")] StephaneLenclud@244: public class EventHid: Ear.Event StephaneLenclud@244: { StephaneLenclud@244: public EventHid() StephaneLenclud@244: { StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public ushort UsagePage { get; set; } StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public ushort UsageCollection { get; set; } StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public ushort Usage { get; set; } StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public Keys Key { get; set; } StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: [Ear.AttributeObjectProperty StephaneLenclud@244: ( StephaneLenclud@244: Id = "HID.Keyboard.IsKeyUp", StephaneLenclud@244: Name = "Key Up", StephaneLenclud@244: Description = "Key up if set, key down otherwise." StephaneLenclud@244: )] StephaneLenclud@244: public bool IsKeyUp { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@246: public bool IsMouse { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@246: public bool IsKeyboard { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@246: public bool IsGeneric { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public bool HasModifierShift { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public bool HasModifierControl { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public bool HasModifierAlt { get; set; } = false; StephaneLenclud@244: StephaneLenclud@244: [DataMember] StephaneLenclud@244: public bool HasModifierWindows { get; set; } = false; StephaneLenclud@244: StephaneLenclud@246: [DataMember] StephaneLenclud@247: public string UsageName { get; set; } = "Press a key"; StephaneLenclud@246: StephaneLenclud@246: StephaneLenclud@244: StephaneLenclud@244: protected override void DoConstruct() StephaneLenclud@244: { StephaneLenclud@244: base.DoConstruct(); StephaneLenclud@244: UpdateDynamicProperties(); StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: private void UpdateDynamicProperties() StephaneLenclud@244: { StephaneLenclud@244: StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: /// StephaneLenclud@244: /// Make sure we distinguish between various configuration of this event StephaneLenclud@244: /// StephaneLenclud@244: /// StephaneLenclud@244: public override string Brief() StephaneLenclud@244: { StephaneLenclud@244: string brief = Name + ": "; StephaneLenclud@244: StephaneLenclud@248: if (!IsValid()) StephaneLenclud@248: { StephaneLenclud@248: brief += "Press a key"; StephaneLenclud@248: return brief; StephaneLenclud@248: } StephaneLenclud@248: StephaneLenclud@244: if (IsKeyboard) StephaneLenclud@244: { StephaneLenclud@244: brief += Key.ToString(); StephaneLenclud@244: StephaneLenclud@244: if (HasModifierAlt) StephaneLenclud@244: { StephaneLenclud@244: brief += " + ALT"; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: if (HasModifierControl) StephaneLenclud@244: { StephaneLenclud@244: brief += " + CTRL"; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: if (HasModifierShift) StephaneLenclud@244: { StephaneLenclud@244: brief += " + SHIFT"; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: if (HasModifierWindows) StephaneLenclud@244: { StephaneLenclud@244: brief += " + WIN"; StephaneLenclud@244: } StephaneLenclud@244: } StephaneLenclud@244: else if (IsGeneric) StephaneLenclud@244: { StephaneLenclud@247: brief += UsageName; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: if (IsKeyUp) StephaneLenclud@244: { StephaneLenclud@244: brief += " (UP)"; StephaneLenclud@244: } StephaneLenclud@244: else StephaneLenclud@244: { StephaneLenclud@244: brief += " (DOWN)"; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: return brief; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: /// StephaneLenclud@244: /// StephaneLenclud@244: /// StephaneLenclud@244: /// StephaneLenclud@244: /// StephaneLenclud@244: public override bool Equals(object obj) StephaneLenclud@244: { StephaneLenclud@247: if (obj is EventHid) StephaneLenclud@244: { StephaneLenclud@247: EventHid e = (EventHid)obj; StephaneLenclud@244: return e.Key == Key StephaneLenclud@247: && e.Usage == Usage StephaneLenclud@247: && e.UsagePage == UsagePage StephaneLenclud@247: && e.UsageCollection == UsageCollection StephaneLenclud@247: && e.IsKeyUp == IsKeyUp StephaneLenclud@247: && e.IsGeneric == IsGeneric StephaneLenclud@247: && e.IsKeyboard == IsKeyboard StephaneLenclud@247: && e.IsMouse == IsMouse StephaneLenclud@247: && e.HasModifierAlt == HasModifierAlt StephaneLenclud@247: && e.HasModifierControl == HasModifierControl StephaneLenclud@247: && e.HasModifierShift == HasModifierShift StephaneLenclud@247: && e.HasModifierWindows == HasModifierWindows; StephaneLenclud@244: } StephaneLenclud@244: StephaneLenclud@244: return false; StephaneLenclud@244: } StephaneLenclud@246: StephaneLenclud@246: StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: protected override void OnStateLeave() StephaneLenclud@246: { StephaneLenclud@246: if (CurrentState == State.Edit) StephaneLenclud@246: { StephaneLenclud@246: // Leaving edit mode StephaneLenclud@246: // Unhook HID events StephaneLenclud@246: Program.HidHandler.OnHidEvent -= HandleHidEvent; StephaneLenclud@246: StephaneLenclud@246: } StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: protected override void OnStateEnter() StephaneLenclud@246: { StephaneLenclud@246: if (CurrentState == State.Edit) StephaneLenclud@246: { StephaneLenclud@246: // Enter edit mode StephaneLenclud@246: // Hook-in HID events StephaneLenclud@246: Program.HidHandler.OnHidEvent += HandleHidEvent; StephaneLenclud@246: StephaneLenclud@246: } StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: /// StephaneLenclud@246: /// Here we receive HID events from our HID library. StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) StephaneLenclud@246: { StephaneLenclud@246: if (CurrentState != State.Edit StephaneLenclud@246: || aHidEvent.IsMouse StephaneLenclud@246: || aHidEvent.IsButtonUp StephaneLenclud@246: || !aHidEvent.IsValid StephaneLenclud@246: || aHidEvent.IsBackground StephaneLenclud@246: || aHidEvent.IsRepeat StephaneLenclud@246: || aHidEvent.IsStray) StephaneLenclud@246: { StephaneLenclud@246: return; StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: PrivateCopy(aHidEvent); StephaneLenclud@246: // StephaneLenclud@246: StephaneLenclud@246: //Tell observer the object itself changed StephaneLenclud@246: OnPropertyChanged("Brief"); StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: public void Copy(Hid.Event aHidEvent) StephaneLenclud@246: { StephaneLenclud@246: PrivateCopy(aHidEvent); StephaneLenclud@246: //We need the key up/down too here StephaneLenclud@246: IsKeyUp = aHidEvent.IsButtonUp; StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: /// StephaneLenclud@246: private void PrivateCopy(Hid.Event aHidEvent) StephaneLenclud@246: { StephaneLenclud@246: //Copy for scan StephaneLenclud@246: UsagePage = aHidEvent.UsagePage; StephaneLenclud@246: UsageCollection = aHidEvent.UsageCollection; StephaneLenclud@246: IsGeneric = aHidEvent.IsGeneric; StephaneLenclud@246: IsKeyboard = aHidEvent.IsKeyboard; StephaneLenclud@246: IsMouse = aHidEvent.IsMouse; StephaneLenclud@246: StephaneLenclud@246: if (IsGeneric) StephaneLenclud@246: { StephaneLenclud@246: if (aHidEvent.Usages.Count > 0) StephaneLenclud@246: { StephaneLenclud@246: Usage = aHidEvent.Usages[0]; StephaneLenclud@247: UsageName = aHidEvent.UsageName(0); StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: Key = Keys.None; StephaneLenclud@246: HasModifierAlt = false; StephaneLenclud@246: HasModifierControl = false; StephaneLenclud@246: HasModifierShift = false; StephaneLenclud@246: HasModifierWindows = false; StephaneLenclud@246: } StephaneLenclud@246: else if (IsKeyboard) StephaneLenclud@246: { StephaneLenclud@246: Usage = 0; StephaneLenclud@246: Key = aHidEvent.VirtualKey; StephaneLenclud@246: HasModifierAlt = aHidEvent.HasModifierAlt; StephaneLenclud@246: HasModifierControl = aHidEvent.HasModifierControl; StephaneLenclud@246: HasModifierShift = aHidEvent.HasModifierShift; StephaneLenclud@246: HasModifierWindows = aHidEvent.HasModifierWindows; StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@246: } StephaneLenclud@246: StephaneLenclud@247: /// StephaneLenclud@247: /// StephaneLenclud@247: /// StephaneLenclud@247: /// StephaneLenclud@247: public override bool IsValid() StephaneLenclud@247: { StephaneLenclud@247: return IsGeneric || IsKeyboard; StephaneLenclud@247: } StephaneLenclud@244: } StephaneLenclud@244: }