Proper UsageId definitions.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
8 namespace Devices.RemoteControl
11 public enum InputDevice
19 public enum RemoteControlButton
76 #region RemoteControlEventArgs
78 public class RemoteControlEventArgs : EventArgs
80 RemoteControlButton _rcb;
83 ConsumerControl iConsumerControl;
85 public RemoteControlEventArgs(RemoteControlButton rcb, InputDevice device)
93 public RemoteControlEventArgs(ConsumerControl aConsumerControl, InputDevice device)
97 iConsumerControl = aConsumerControl;
102 public RemoteControlEventArgs(MceButton mce, InputDevice device)
110 private void SetNullButtons()
112 iConsumerControl = ConsumerControl.Null;
113 iMceButton = MceButton.Null;
114 _rcb = RemoteControlButton.Unknown;
117 public RemoteControlEventArgs()
119 iMceButton = MceButton.Null;
120 _rcb = RemoteControlButton.Unknown;
121 _device = InputDevice.Key;
124 public RemoteControlButton Button
127 set { _rcb = value; }
130 public MceButton MceButton
132 get { return iMceButton; }
133 set { iMceButton = value; }
136 public ConsumerControl ConsumerControl
138 get { return iConsumerControl; }
139 set { iConsumerControl = value; }
142 public InputDevice Device
144 get { return _device; }
145 set { _device = value; }
149 #endregion RemoteControlEventArgs
152 public sealed class RemoteControlDevice
154 public delegate bool RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
155 public event RemoteControlDeviceEventHandler ButtonPressed;
158 /// Return true if the usage was processed.
160 /// <param name="aUsage"></param>
161 /// <returns></returns>
162 public delegate bool HidUsageHandler(ushort aUsage);
165 //-------------------------------------------------------------
167 //-------------------------------------------------------------
169 public RemoteControlDevice(IntPtr aHWND)
171 // Register the input device to receive the commands from the
172 // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
173 // for the vendor defined usage page.
175 RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[3];
178 rid[i].usUsagePage = (ushort)Hid.UsagePage.MceRemote;
179 rid[i].usUsage = (ushort)Hid.UsageIdMce.MceRemote;
180 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
181 rid[i].hwndTarget = aHWND;
184 rid[i].usUsagePage = (ushort)Hid.UsagePage.Consumer;
185 rid[i].usUsage = (ushort)Hid.UsageIdConsumer.ConsumerControl;
186 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
187 rid[i].hwndTarget = aHWND;
190 rid[i].usUsagePage = (ushort)Hid.UsagePage.Consumer;
191 rid[i].usUsage = (ushort)Hid.UsageIdConsumer.Selection;
192 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
193 rid[i].hwndTarget = aHWND;
196 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
197 //rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.SystemControl;
198 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
199 //rid[i].hwndTarget = aHWND;
202 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
203 //rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.Keyboard;
204 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
205 //rid[i].hwndTarget = aHWND;
208 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
209 //rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.Mouse;
210 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
211 //rid[i].hwndTarget = aHWND;
214 if (!Function.RegisterRawInputDevices(rid,(uint) rid.Length,(uint) Marshal.SizeOf(rid[0])))
216 throw new ApplicationException("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
221 //-------------------------------------------------------------
223 //-------------------------------------------------------------
225 public void ProcessMessage(Message message)
229 case Const.WM_KEYDOWN:
230 ProcessKeyDown(message.WParam);
233 //Returning zero means we processed that message.
234 message.Result = new IntPtr(0);
235 ProcessInputCommand(ref message);
242 //-------------------------------------------------------------
244 //-------------------------------------------------------------
246 private void ProcessKeyDown(IntPtr wParam)
248 RemoteControlButton rcb = RemoteControlButton.Unknown;
250 switch (wParam.ToInt32())
252 case (int) Keys.Escape:
253 rcb = RemoteControlButton.Clear;
255 case (int) Keys.Down:
256 rcb = RemoteControlButton.Down;
258 case (int) Keys.Left:
259 rcb = RemoteControlButton.Left;
262 rcb = RemoteControlButton.Digit0;
265 rcb = RemoteControlButton.Digit1;
268 rcb = RemoteControlButton.Digit2;
271 rcb = RemoteControlButton.Digit3;
274 rcb = RemoteControlButton.Digit4;
277 rcb = RemoteControlButton.Digit5;
280 rcb = RemoteControlButton.Digit6;
283 rcb = RemoteControlButton.Digit7;
286 rcb = RemoteControlButton.Digit8;
289 rcb = RemoteControlButton.Digit9;
291 case (int) Keys.Enter:
292 rcb = RemoteControlButton.Enter;
294 case (int) Keys.Right:
295 rcb = RemoteControlButton.Right;
298 rcb = RemoteControlButton.Up;
302 if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
303 this.ButtonPressed(this, new RemoteControlEventArgs(rcb, InputDevice.Key));
310 /// <param name="aUsage"></param>
311 private bool HidConsumerDeviceHandler(ushort aUsage)
319 if (Enum.IsDefined(typeof(ConsumerControl), aUsage) && aUsage != 0) //Our button is a known consumer control
321 if (this.ButtonPressed != null)
323 return this.ButtonPressed(this, new RemoteControlEventArgs((ConsumerControl)aUsage, InputDevice.OEM));
329 Debug.WriteLine("Unknown Consumer Control!");
337 /// <param name="aUsage"></param>
338 private bool HidMceRemoteHandler(ushort aUsage)
347 if (Enum.IsDefined(typeof(MceButton), aUsage) && aUsage != 0) //Our button is a known MCE button
349 if (this.ButtonPressed != null)
351 return this.ButtonPressed(this, new RemoteControlEventArgs((MceButton)aUsage, InputDevice.OEM));
357 Debug.WriteLine("Unknown MCE button!");
363 private void ProcessInputCommand(ref Message message)
365 //We received a WM_INPUT message
366 Debug.WriteLine("================WM_INPUT================");
368 //Check if we received this message while in background or foreground
369 if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUT)
371 Debug.WriteLine("================FOREGROUND");
373 else if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUTSINK)
375 Debug.WriteLine("================BACKGROUND");
378 //Declare some pointers
379 IntPtr rawInputBuffer = IntPtr.Zero;
380 //My understanding is that this is basically our HID descriptor
381 IntPtr preParsedData = IntPtr.Zero;
386 RAWINPUT rawInput = new RAWINPUT();
387 if (!RawInput.GetRawInputData(message.LParam, ref rawInput, ref rawInputBuffer))
393 RID_DEVICE_INFO deviceInfo = new RID_DEVICE_INFO();
394 if (!RawInput.GetDeviceInfo(rawInput.header.hDevice, ref deviceInfo))
400 if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID
402 Debug.WriteLine("WM_INPUT source device is HID.");
403 //Get Usage Page and Usage
404 Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage ID: 0x" + deviceInfo.hid.usUsage.ToString("X4"));
407 preParsedData = RawInput.GetPreParsedData(rawInput.header.hDevice);
410 HidUsageHandler usagePageHandler=null;
412 //Check if this an MCE remote HID message
413 if (deviceInfo.hid.usUsagePage == (ushort)Hid.UsagePage.MceRemote && deviceInfo.hid.usUsage == (ushort)Hid.UsageIdMce.MceRemote)
415 usagePageHandler = HidMceRemoteHandler;
417 //Check if this is a consumer control HID message
418 else if (deviceInfo.hid.usUsagePage == (ushort)Hid.UsagePage.Consumer && deviceInfo.hid.usUsage == (ushort)Hid.UsageIdConsumer.ConsumerControl)
420 usagePageHandler = HidConsumerDeviceHandler;
422 //Unknown HID message
425 Debug.WriteLine("Unknown HID message.");
429 if (!(rawInput.hid.dwSizeHid > 1 //Make sure our HID msg size more than 1. In fact the first ushort is irrelevant to us for now
430 && rawInput.hid.dwCount > 0)) //Check that we have at least one HID msg
436 //Allocate a buffer for one HID input
437 byte[] hidInputReport = new byte[rawInput.hid.dwSizeHid];
439 Debug.WriteLine("Raw input contains " + rawInput.hid.dwCount + " HID input report(s)");
441 //For each HID input report in our raw input
442 for (int i = 0; i < rawInput.hid.dwCount; i++)
444 //Compute the address from which to copy our HID input
445 int hidInputOffset = 0;
448 byte* source = (byte*)rawInputBuffer;
449 source += sizeof(RAWINPUTHEADER) + sizeof(RAWHID) + (rawInput.hid.dwSizeHid * i);
450 hidInputOffset = (int)source;
453 //Copy HID input into our buffer
454 Marshal.Copy(new IntPtr(hidInputOffset), hidInputReport, 0, (int)rawInput.hid.dwSizeHid);
456 //Print HID input report in our debug output
457 string hidDump = "HID input report: ";
458 foreach (byte b in hidInputReport)
460 hidDump += b.ToString("X2");
462 Debug.WriteLine(hidDump);
465 uint usageCount = 1; //Assuming a single usage per input report. Is that correct?
466 Win32.USAGE_AND_PAGE[] usages = new Win32.USAGE_AND_PAGE[usageCount];
467 Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, hidInputReport, (uint)hidInputReport.Length);
468 if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
470 Debug.WriteLine("Could not parse HID data!");
474 Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
475 Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
476 //Call on our Usage Page handler
477 usagePageHandler(usages[0].Usage);
482 else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE)
484 Debug.WriteLine("WM_INPUT source device is Mouse.");
485 // do mouse handling...
487 else if (rawInput.header.dwType == Const.RIM_TYPEKEYBOARD)
489 Debug.WriteLine("WM_INPUT source device is Keyboard.");
490 // do keyboard handling...
496 //Always executed when leaving our try block
497 Marshal.FreeHGlobal(rawInputBuffer);
498 Marshal.FreeHGlobal(preParsedData);