Hid/HidHandler.cs
changeset 83 2d5955694057
parent 81 baabcd5cdf8c
child 84 e13ea80016a1
     1.1 --- a/Hid/HidHandler.cs	Sun Mar 15 21:26:51 2015 +0100
     1.2 +++ b/Hid/HidHandler.cs	Tue Mar 17 15:35:58 2015 +0100
     1.3 @@ -33,19 +33,40 @@
     1.4      /// <summary>
     1.5      /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
     1.6      /// </summary>
     1.7 -    public class Handler
     1.8 +    public class Handler : IDisposable
     1.9      {
    1.10          public delegate void HidEventHandler(object aSender, Event aHidEvent);
    1.11          public event HidEventHandler OnHidEvent;
    1.12          List<Event> iHidEvents;
    1.13 +        RAWINPUTDEVICE[] iRawInputDevices;
    1.14  
    1.15  
    1.16          public bool IsRegistered { get; private set; }
    1.17 +        public bool ManageRepeats { get; private set; }
    1.18  
    1.19 -        public Handler(RAWINPUTDEVICE[] aRawInputDevices)
    1.20 +        public Handler(RAWINPUTDEVICE[] aRawInputDevices, bool aManageRepeats=false)
    1.21          {
    1.22 -            iHidEvents=new List<Event>();
    1.23 -            IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
    1.24 +            iRawInputDevices = aRawInputDevices;
    1.25 +            iHidEvents = new List<Event>();
    1.26 +            IsRegistered = Function.RegisterRawInputDevices(iRawInputDevices, (uint)iRawInputDevices.Length, (uint)Marshal.SizeOf(iRawInputDevices[0]));
    1.27 +            ManageRepeats = aManageRepeats;
    1.28 +        }
    1.29 +
    1.30 +        /// <summary>
    1.31 +        /// Will de-register devices.
    1.32 +        /// </summary>
    1.33 +        public void Dispose()
    1.34 +        {
    1.35 +            //Setup device removal
    1.36 +            for (int i=0; i<iRawInputDevices.Length; i++)
    1.37 +            {
    1.38 +                iRawInputDevices[i].dwFlags = Const.RIDEV_REMOVE;
    1.39 +                iRawInputDevices[i].hwndTarget = IntPtr.Zero;
    1.40 +            }
    1.41 +
    1.42 +            //De-register
    1.43 +            Function.RegisterRawInputDevices(iRawInputDevices, (uint)iRawInputDevices.Length, (uint)Marshal.SizeOf(iRawInputDevices[0]));
    1.44 +            IsRegistered = false;
    1.45          }
    1.46  
    1.47          /// <summary>
    1.48 @@ -60,7 +81,7 @@
    1.49                  return;
    1.50              }
    1.51  
    1.52 -            Event hidEvent = new Event(aMessage, OnHidEventRepeat);
    1.53 +            Event hidEvent = new Event(aMessage, OnHidEventRepeat, ManageRepeats);
    1.54              hidEvent.DebugWrite();
    1.55  
    1.56              if (!hidEvent.IsValid || !hidEvent.IsGeneric)
    1.57 @@ -70,23 +91,26 @@
    1.58              }
    1.59  
    1.60              //
    1.61 -            if (hidEvent.IsButtonUp)
    1.62 +            if (ManageRepeats)
    1.63              {
    1.64 -                //This is a key up event
    1.65 -                //We need to discard any events belonging to the same page and collection
    1.66 -                for (int i = (iHidEvents.Count-1); i >= 0; i--)
    1.67 +                if (hidEvent.IsButtonUp)
    1.68                  {
    1.69 -                    if (iHidEvents[i].UsageId == hidEvent.UsageId)
    1.70 +                    //This is a key up event
    1.71 +                    //We need to discard any events belonging to the same page and collection
    1.72 +                    for (int i = (iHidEvents.Count - 1); i >= 0; i--)
    1.73                      {
    1.74 -                        iHidEvents[i].Dispose();
    1.75 -                        iHidEvents.RemoveAt(i);
    1.76 +                        if (iHidEvents[i].UsageId == hidEvent.UsageId)
    1.77 +                        {
    1.78 +                            iHidEvents[i].Dispose();
    1.79 +                            iHidEvents.RemoveAt(i);
    1.80 +                        }
    1.81                      }
    1.82                  }
    1.83 -            }
    1.84 -            else
    1.85 -            {
    1.86 -                //Keep that event until we get a key up message
    1.87 -                iHidEvents.Add(hidEvent);
    1.88 +                else
    1.89 +                {
    1.90 +                    //Keep that event until we get a key up message
    1.91 +                    iHidEvents.Add(hidEvent);
    1.92 +                }
    1.93              }
    1.94  
    1.95              //Broadcast our events