HidHandler.cs
author sl
Tue, 23 Dec 2014 23:05:23 +0100
changeset 41 dd603eba46ca
parent 40 b3e177062849
child 49 2ec781e51f36
permissions -rw-r--r--
Implementing key repeat support.
sl@29
     1
using System;
sl@29
     2
using System.Windows.Forms;
sl@29
     3
using System.Runtime.InteropServices;
sl@29
     4
using System.Diagnostics;
sl@29
     5
using System.Text;
sl@29
     6
using Microsoft.Win32.SafeHandles;
sl@29
     7
using Win32;
sl@29
     8
using System.Collections.Generic;
sl@29
     9
sl@29
    10
sl@29
    11
namespace Hid
sl@29
    12
{
sl@29
    13
sl@29
    14
sl@29
    15
    /// <summary>
sl@29
    16
    /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
sl@29
    17
    /// </summary>
sl@40
    18
    public class HidHandler
sl@29
    19
    {
sl@29
    20
        public delegate void HidEventHandler(object aSender, HidEvent aHidEvent);
sl@29
    21
        public event HidEventHandler OnHidEvent;
sl@41
    22
        List<HidEvent> iHidEvents;
sl@41
    23
sl@29
    24
sl@29
    25
        public bool IsRegistered { get; private set; }
sl@29
    26
sl@29
    27
        public HidHandler(RAWINPUTDEVICE[] aRawInputDevices)
sl@29
    28
        {
sl@41
    29
            iHidEvents=new List<HidEvent>();
sl@29
    30
            IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
sl@29
    31
        }
sl@29
    32
sl@29
    33
        public void ProcessInput(Message aMessage)
sl@29
    34
        {
sl@41
    35
            Hid.HidEvent hidEvent = new Hid.HidEvent(aMessage, OnHidEventRepeat);
sl@29
    36
            hidEvent.DebugWrite();
sl@29
    37
sl@29
    38
            if (!hidEvent.IsValid || !hidEvent.IsGeneric)
sl@29
    39
            {
sl@29
    40
                Debug.WriteLine("Skipping HID message.");
sl@29
    41
                return;
sl@29
    42
            }
sl@29
    43
sl@41
    44
            //
sl@41
    45
            if (hidEvent.Usages[0] == 0)
sl@41
    46
            {
sl@41
    47
                //This is a key up event
sl@41
    48
                //We need to discard any events belonging to the same page and collection
sl@41
    49
                for (int i = (iHidEvents.Count-1); i >= 0; i--)
sl@41
    50
                {
sl@41
    51
                    if (iHidEvents[i].UsageId == hidEvent.UsageId)
sl@41
    52
                    {
sl@41
    53
                        iHidEvents[i].Dispose();
sl@41
    54
                        iHidEvents.RemoveAt(i);
sl@41
    55
                    }
sl@41
    56
                }
sl@41
    57
            }
sl@41
    58
            else
sl@41
    59
            {
sl@41
    60
                //Keep that event until we get a key up message
sl@41
    61
                iHidEvents.Add(hidEvent);
sl@41
    62
            }
sl@41
    63
sl@29
    64
            //Broadcast our events
sl@29
    65
            OnHidEvent(this, hidEvent);    
sl@29
    66
        }
sl@29
    67
sl@41
    68
        public void OnHidEventRepeat(HidEvent aHidEvent)
sl@41
    69
        {
sl@41
    70
            //Broadcast our events
sl@41
    71
            OnHidEvent(this, aHidEvent);    
sl@41
    72
        }
sl@41
    73
sl@29
    74
    }
sl@29
    75
sl@29
    76
}