sl@29: using System; sl@29: using System.Windows.Forms; sl@29: using System.Runtime.InteropServices; sl@29: using System.Diagnostics; sl@29: using System.Text; sl@29: using Microsoft.Win32.SafeHandles; sl@29: using Win32; sl@29: using System.Collections.Generic; sl@29: sl@29: sl@29: namespace Hid sl@29: { sl@29: /// sl@29: /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return. sl@29: /// sl@40: public class HidHandler sl@29: { sl@29: public delegate void HidEventHandler(object aSender, HidEvent aHidEvent); sl@29: public event HidEventHandler OnHidEvent; sl@41: List iHidEvents; sl@41: sl@29: sl@29: public bool IsRegistered { get; private set; } sl@29: sl@29: public HidHandler(RAWINPUTDEVICE[] aRawInputDevices) sl@29: { sl@41: iHidEvents=new List(); sl@29: IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0])); sl@29: } sl@29: sl@29: public void ProcessInput(Message aMessage) sl@29: { sl@41: Hid.HidEvent hidEvent = new Hid.HidEvent(aMessage, OnHidEventRepeat); sl@29: hidEvent.DebugWrite(); sl@29: sl@29: if (!hidEvent.IsValid || !hidEvent.IsGeneric) sl@29: { sl@29: Debug.WriteLine("Skipping HID message."); sl@29: return; sl@29: } sl@29: sl@41: // StephaneLenclud@49: if (hidEvent.IsButtonUp) sl@41: { sl@41: //This is a key up event sl@41: //We need to discard any events belonging to the same page and collection sl@41: for (int i = (iHidEvents.Count-1); i >= 0; i--) sl@41: { sl@41: if (iHidEvents[i].UsageId == hidEvent.UsageId) sl@41: { sl@41: iHidEvents[i].Dispose(); sl@41: iHidEvents.RemoveAt(i); sl@41: } sl@41: } sl@41: } sl@41: else sl@41: { sl@41: //Keep that event until we get a key up message sl@41: iHidEvents.Add(hidEvent); sl@41: } sl@41: sl@29: //Broadcast our events sl@29: OnHidEvent(this, hidEvent); sl@29: } sl@29: sl@41: public void OnHidEventRepeat(HidEvent aHidEvent) sl@41: { sl@41: //Broadcast our events sl@41: OnHidEvent(this, aHidEvent); sl@41: } sl@41: sl@29: } sl@29: sl@29: }