# HG changeset patch # User StephaneLenclud # Date 1471538943 -7200 # Node ID 3b5a94f31400d263ef58785471f2660ea426a78a # Parent 5c4f1e2bf29a664188a276b4b133ffb6b86203f5 Adding support for HID Keyboard events. diff -r 5c4f1e2bf29a -r 3b5a94f31400 Server/Events/EventHidKeyboard.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/Events/EventHidKeyboard.cs Thu Aug 18 18:49:03 2016 +0200 @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; +using System.Windows.Forms; +using Ear = SharpLib.Ear; +using Hid = SharpLib.Hid; + +namespace SharpDisplayManager +{ + [DataContract] + [Ear.AttributeObject(Id = "Event.Hid.Keyboard", Name = "HID Keyboard", Description = "Corresponding HID message received.")] + public class EventHidKeyboard : Ear.Event + { + public EventHidKeyboard() + { + } + + [DataMember] + [Ear.AttributeObjectProperty + ( + Id = "HID.Keyboard.Key", + Name = "Key", + Description = "The virtual key code." + )] + public Keys Key { get; set; } + + [DataMember] + [Ear.AttributeObjectProperty + ( + Id = "HID.Keyboard.IsKeyUp", + Name = "Key Up", + Description = "Key up if set, key down otherwise." + )] + public bool IsKeyUp { get; set; } = true; + + [DataMember] + [Ear.AttributeObjectProperty + ( + Id = "HID.Keyboard.HasModifierShift", + Name = "Shift", + Description = "Shift modifier applied." + )] + public bool HasModifierShift { get; set; } = false; + + [DataMember] + [Ear.AttributeObjectProperty + ( + Id = "HID.Keyboard.HasModifierControl", + Name = "Control", + Description = "Control modifier applied." + )] + public bool HasModifierControl { get; set; } = false; + + [DataMember] + [Ear.AttributeObjectProperty + ( + Id = "HID.Keyboard.HasModifierAlt", + Name = "Alt", + Description = "Alt modifier applied." + )] + public bool HasModifierAlt { get; set; } = false; + + [DataMember] + [Ear.AttributeObjectProperty + ( + Id = "HID.Keyboard.HasModifierWindows", + Name = "Windows", + Description = "Windows modifier applied." + )] + public bool HasModifierWindows { get; set; } = false; + + + + /// + /// Make sure we distinguish between various configuration of this event + /// + /// + public override string Brief() + { + string brief = Name + ": " + Key.ToString(); + + if (IsKeyUp) + { + brief += " (UP)"; + } + else + { + brief += " (DOWN)"; + } + + if (HasModifierAlt) + { + brief += " + ALT"; + } + + if (HasModifierControl) + { + brief += " + CTRL"; + } + + if (HasModifierShift) + { + brief += " + SHIFT"; + } + + if (HasModifierWindows) + { + brief += " + WIN"; + } + + return brief; + } + + /// + /// + /// + /// + /// + public override bool Equals(object obj) + { + if (obj is EventHidKeyboard) + { + EventHidKeyboard e = (EventHidKeyboard)obj; + return e.Key == Key + && e.IsKeyUp == IsKeyUp + && e.HasModifierAlt == HasModifierAlt + && e.HasModifierControl == HasModifierControl + && e.HasModifierShift == HasModifierShift + && e.HasModifierWindows == HasModifierWindows; + } + + return false; + } + } +} diff -r 5c4f1e2bf29a -r 3b5a94f31400 Server/FormMain.Hid.cs --- a/Server/FormMain.Hid.cs Thu Aug 18 17:43:03 2016 +0200 +++ b/Server/FormMain.Hid.cs Thu Aug 18 18:49:03 2016 +0200 @@ -40,7 +40,7 @@ // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp // for the vendor defined usage page. - RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5]; + RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[6]; int i = 0; rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; @@ -72,6 +72,12 @@ rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK; rid[i].hwndTarget = Handle; + i++; + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard; + rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK; + rid[i].hwndTarget = Handle; + //i++; //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard; @@ -104,7 +110,7 @@ /// public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) { - if (aHidEvent.IsStray) + if (aHidEvent.IsStray || !aHidEvent.IsValid) { //Stray event just ignore it return; @@ -118,43 +124,67 @@ } else { - if (aHidEvent.Usages.Count == 0) + if (aHidEvent.IsGeneric) { - //No usage, nothing to do then - return; - } + if (aHidEvent.Usages.Count == 0) + { + //No usage, nothing to do then + return; + } - //We are in the proper thread - if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl) - { - //Trigger events as needed - EventHidWindowsMediaCenter e = new EventHidWindowsMediaCenter { Usage = (Hid.Usage.WindowsMediaCenterRemoteControl)aHidEvent.Usages[0] }; - Properties.Settings.Default.EarManager.TriggerEvent(e); + //We are in the proper thread + if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl) + { + //Trigger events as needed + EventHidWindowsMediaCenter e = new EventHidWindowsMediaCenter + { + Usage = (Hid.Usage.WindowsMediaCenterRemoteControl) aHidEvent.Usages[0] + }; + Properties.Settings.Default.EarManager.TriggerEvent(e); - //Old legacy hard coded stuff - //TODO: remove it - switch (aHidEvent.Usages[0]) + //Old legacy hard coded stuff + //TODO: remove it + switch (aHidEvent.Usages[0]) + { + case (ushort) Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart: + HandleGreenStart(); + break; + case (ushort) Hid.Usage.WindowsMediaCenterRemoteControl.Eject: + case (ushort) Hid.Usage.WindowsMediaCenterRemoteControl.Ext2: + HandleEject(); + break; + } + } + else if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.Consumer) { - case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart: - HandleGreenStart(); - break; - case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject: - case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2: - HandleEject(); - break; + //Trigger matching events if any + EventHidConsumerControl e = new EventHidConsumerControl + { + Usage = (Hid.Usage.ConsumerControl) aHidEvent.Usages[0] + }; + Properties.Settings.Default.EarManager.TriggerEvent(e); + + //Keep this for debug when only ThinkPad keyboard is available + //if (aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier) + //{ + // HandleEject(); + //} } } - else if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer) + else if (aHidEvent.IsKeyboard) { - EventHidConsumerControl e = new EventHidConsumerControl { Usage = (Hid.Usage.ConsumerControl)aHidEvent.Usages[0] }; + //Trigger matching events if any + EventHidKeyboard e = new EventHidKeyboard + { + Key = aHidEvent.VirtualKey, + IsKeyUp = aHidEvent.IsButtonUp, + HasModifierAlt = aHidEvent.HasModifierAlt, + HasModifierControl = aHidEvent.HasModifierControl, + HasModifierShift = aHidEvent.HasModifierShift, + HasModifierWindows = aHidEvent.HasModifierWindows, + }; Properties.Settings.Default.EarManager.TriggerEvent(e); - - //Keep this for debug when only ThinkPad keyboard is available - //if (aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier) - //{ - // HandleEject(); - //} - } + } } } diff -r 5c4f1e2bf29a -r 3b5a94f31400 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Thu Aug 18 17:43:03 2016 +0200 +++ b/Server/SharpDisplayManager.csproj Thu Aug 18 18:49:03 2016 +0200 @@ -180,6 +180,7 @@ + Form