Renaming demo application to HID Demo.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
8 using System.Collections.Generic;
14 /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
16 public class HidHandler
18 public delegate void HidEventHandler(object aSender, HidEvent aHidEvent);
19 public event HidEventHandler OnHidEvent;
20 List<HidEvent> iHidEvents;
23 public bool IsRegistered { get; private set; }
25 public HidHandler(RAWINPUTDEVICE[] aRawInputDevices)
27 iHidEvents=new List<HidEvent>();
28 IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
31 public void ProcessInput(Message aMessage)
33 Hid.HidEvent hidEvent = new Hid.HidEvent(aMessage, OnHidEventRepeat);
34 hidEvent.DebugWrite();
36 if (!hidEvent.IsValid || !hidEvent.IsGeneric)
38 Debug.WriteLine("Skipping HID message.");
43 if (hidEvent.IsButtonUp)
45 //This is a key up event
46 //We need to discard any events belonging to the same page and collection
47 for (int i = (iHidEvents.Count-1); i >= 0; i--)
49 if (iHidEvents[i].UsageId == hidEvent.UsageId)
51 iHidEvents[i].Dispose();
52 iHidEvents.RemoveAt(i);
58 //Keep that event until we get a key up message
59 iHidEvents.Add(hidEvent);
62 //Broadcast our events
63 OnHidEvent(this, hidEvent);
66 public void OnHidEventRepeat(HidEvent aHidEvent)
68 //Broadcast our events
69 OnHidEvent(this, aHidEvent);