HID Usage Tables fixes.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
13 namespace Devices.RemoteControl
16 public enum InputDevice
24 public enum RemoteControlButton
81 #region RemoteControlEventArgs
83 public class RemoteControlEventArgs : EventArgs
85 RemoteControlButton _rcb;
88 ConsumerControl iConsumerControl;
90 public RemoteControlEventArgs(RemoteControlButton rcb, InputDevice device)
98 public RemoteControlEventArgs(ConsumerControl aConsumerControl, InputDevice device)
102 iConsumerControl = aConsumerControl;
107 public RemoteControlEventArgs(MceButton mce, InputDevice device)
115 private void SetNullButtons()
117 iConsumerControl = ConsumerControl.Null;
118 iMceButton = MceButton.Null;
119 _rcb = RemoteControlButton.Unknown;
122 public RemoteControlEventArgs()
124 iMceButton = MceButton.Null;
125 _rcb = RemoteControlButton.Unknown;
126 _device = InputDevice.Key;
129 public RemoteControlButton Button
132 set { _rcb = value; }
135 public MceButton MceButton
137 get { return iMceButton; }
138 set { iMceButton = value; }
141 public ConsumerControl ConsumerControl
143 get { return iConsumerControl; }
144 set { iConsumerControl = value; }
147 public InputDevice Device
149 get { return _device; }
150 set { _device = value; }
154 #endregion RemoteControlEventArgs
157 public sealed class RemoteControlDevice
159 public delegate bool RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
160 public event RemoteControlDeviceEventHandler ButtonPressed;
163 /// Return true if the usage was processed.
165 /// <param name="aUsage"></param>
166 /// <returns></returns>
167 public delegate bool HidUsageHandler(ushort aUsage);
170 //-------------------------------------------------------------
172 //-------------------------------------------------------------
174 public RemoteControlDevice(IntPtr aHWND)
176 // Register the input device to receive the commands from the
177 // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
178 // for the vendor defined usage page.
180 RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[4];
183 rid[i].usUsagePage = (ushort)Hid.UsagePage.MceRemote;
184 rid[i].usUsage = (ushort)Hid.UsageIdMce.MceRemote;
185 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
186 rid[i].hwndTarget = aHWND;
189 rid[i].usUsagePage = (ushort)Hid.UsagePage.Consumer;
190 rid[i].usUsage = (ushort)Hid.UsageCollectionConsumer.ConsumerControl;
191 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
192 rid[i].hwndTarget = aHWND;
195 rid[i].usUsagePage = (ushort)Hid.UsagePage.Consumer;
196 rid[i].usUsage = (ushort)Hid.UsageCollectionConsumer.Selection;
197 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
198 rid[i].hwndTarget = aHWND;
201 rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
202 rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.SystemControl;
203 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
204 rid[i].hwndTarget = aHWND;
207 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
208 //rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.Keyboard;
209 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
210 //rid[i].hwndTarget = aHWND;
213 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
214 //rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.Mouse;
215 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
216 //rid[i].hwndTarget = aHWND;
219 if (!Function.RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0])))
221 throw new ApplicationException("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
226 //-------------------------------------------------------------
228 //-------------------------------------------------------------
230 public void ProcessMessage(Message message)
234 case Const.WM_KEYDOWN:
235 ProcessKeyDown(message.WParam);
238 //Returning zero means we processed that message.
239 message.Result = new IntPtr(0);
240 ProcessInputCommand(ref message);
247 //-------------------------------------------------------------
249 //-------------------------------------------------------------
251 private void ProcessKeyDown(IntPtr wParam)
253 RemoteControlButton rcb = RemoteControlButton.Unknown;
255 switch (wParam.ToInt32())
257 case (int)Keys.Escape:
258 rcb = RemoteControlButton.Clear;
261 rcb = RemoteControlButton.Up;
264 rcb = RemoteControlButton.Down;
267 rcb = RemoteControlButton.Left;
269 case (int)Keys.Right:
270 rcb = RemoteControlButton.Right;
272 case (int)Keys.Enter:
273 rcb = RemoteControlButton.Enter;
276 rcb = RemoteControlButton.Digit0;
279 rcb = RemoteControlButton.Digit1;
282 rcb = RemoteControlButton.Digit2;
285 rcb = RemoteControlButton.Digit3;
288 rcb = RemoteControlButton.Digit4;
291 rcb = RemoteControlButton.Digit5;
294 rcb = RemoteControlButton.Digit6;
297 rcb = RemoteControlButton.Digit7;
300 rcb = RemoteControlButton.Digit8;
303 rcb = RemoteControlButton.Digit9;
307 if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
309 Debug.WriteLine("KeyDown: " + rcb.ToString());
310 this.ButtonPressed(this, new RemoteControlEventArgs(rcb, InputDevice.Key));
318 /// <param name="aUsage"></param>
319 private bool HidConsumerDeviceHandler(ushort aUsage)
327 if (Enum.IsDefined(typeof(ConsumerControl), aUsage) && aUsage != 0) //Our button is a known consumer control
329 if (this.ButtonPressed != null)
331 return this.ButtonPressed(this, new RemoteControlEventArgs((ConsumerControl)aUsage, InputDevice.OEM));
337 Debug.WriteLine("Unknown Consumer Control!");
345 /// <param name="aUsage"></param>
346 private bool HidMceRemoteHandler(ushort aUsage)
355 if (Enum.IsDefined(typeof(MceButton), aUsage) && aUsage != 0) //Our button is a known MCE button
357 if (this.ButtonPressed != null)
359 return this.ButtonPressed(this, new RemoteControlEventArgs((MceButton)aUsage, InputDevice.OEM));
365 Debug.WriteLine("Unknown MCE button!");
371 private void ProcessInputCommand(ref Message message)
373 //We received a WM_INPUT message
374 Debug.WriteLine("================WM_INPUT================");
376 Hid.HidEvent hidEvent = new Hid.HidEvent(message);
377 hidEvent.DebugWrite();
379 if (!hidEvent.IsValid || !hidEvent.IsGeneric)
381 Debug.WriteLine("Skipping HID message.");
386 HidUsageHandler usagePageHandler = null;
388 //Check if this an MCE remote HID message
389 if (hidEvent.UsagePage == (ushort)Hid.UsagePage.MceRemote && hidEvent.UsageCollection == (ushort)Hid.UsageIdMce.MceRemote)
391 usagePageHandler = HidMceRemoteHandler;
393 //Check if this is a consumer control HID message
394 else if (hidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer && hidEvent.UsageCollection == (ushort)Hid.UsageCollectionConsumer.ConsumerControl)
396 usagePageHandler = HidConsumerDeviceHandler;
398 //Unknown HID message
401 Debug.WriteLine("Unknown HID message.");
405 foreach (ushort usage in hidEvent.Usages)
407 usagePageHandler(usage);