HidHandler.cs
author sl
Sat, 06 Dec 2014 23:34:02 +0100
changeset 29 7679a5ab194b
child 40 b3e177062849
permissions -rw-r--r--
Adding HID handler.
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@29
    18
    class HidHandler
sl@29
    19
    {
sl@29
    20
        public delegate void HidEventHandler(object aSender, HidEvent aHidEvent);
sl@29
    21
        public event HidEventHandler OnHidEvent;
sl@29
    22
sl@29
    23
        public bool IsRegistered { get; private set; }
sl@29
    24
sl@29
    25
        public HidHandler(RAWINPUTDEVICE[] aRawInputDevices)
sl@29
    26
        {
sl@29
    27
            IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
sl@29
    28
        }
sl@29
    29
sl@29
    30
sl@29
    31
        public void ProcessInput(Message aMessage)
sl@29
    32
        {
sl@29
    33
            Hid.HidEvent hidEvent = new Hid.HidEvent(aMessage);
sl@29
    34
            hidEvent.DebugWrite();
sl@29
    35
sl@29
    36
            if (!hidEvent.IsValid || !hidEvent.IsGeneric)
sl@29
    37
            {
sl@29
    38
                Debug.WriteLine("Skipping HID message.");
sl@29
    39
                return;
sl@29
    40
            }
sl@29
    41
sl@29
    42
            //Broadcast our events
sl@29
    43
            OnHidEvent(this, hidEvent);    
sl@29
    44
        }
sl@29
    45
sl@29
    46
    }
sl@29
    47
sl@29
    48
}