author | StephaneLenclud |
Sun, 15 Feb 2015 13:30:54 +0100 | |
changeset 59 | 89dfad9633b2 |
parent 41 | dd603eba46ca |
child 76 | 831ebeeecfdf |
permissions | -rw-r--r-- |
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 |
/// <summary> |
sl@29 | 14 |
/// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return. |
sl@29 | 15 |
/// </summary> |
sl@40 | 16 |
public class HidHandler |
sl@29 | 17 |
{ |
sl@29 | 18 |
public delegate void HidEventHandler(object aSender, HidEvent aHidEvent); |
sl@29 | 19 |
public event HidEventHandler OnHidEvent; |
sl@41 | 20 |
List<HidEvent> iHidEvents; |
sl@41 | 21 |
|
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@41 | 27 |
iHidEvents=new List<HidEvent>(); |
sl@29 | 28 |
IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0])); |
sl@29 | 29 |
} |
sl@29 | 30 |
|
sl@29 | 31 |
public void ProcessInput(Message aMessage) |
sl@29 | 32 |
{ |
sl@41 | 33 |
Hid.HidEvent hidEvent = new Hid.HidEvent(aMessage, OnHidEventRepeat); |
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@41 | 42 |
// |
StephaneLenclud@49 | 43 |
if (hidEvent.IsButtonUp) |
sl@41 | 44 |
{ |
sl@41 | 45 |
//This is a key up event |
sl@41 | 46 |
//We need to discard any events belonging to the same page and collection |
sl@41 | 47 |
for (int i = (iHidEvents.Count-1); i >= 0; i--) |
sl@41 | 48 |
{ |
sl@41 | 49 |
if (iHidEvents[i].UsageId == hidEvent.UsageId) |
sl@41 | 50 |
{ |
sl@41 | 51 |
iHidEvents[i].Dispose(); |
sl@41 | 52 |
iHidEvents.RemoveAt(i); |
sl@41 | 53 |
} |
sl@41 | 54 |
} |
sl@41 | 55 |
} |
sl@41 | 56 |
else |
sl@41 | 57 |
{ |
sl@41 | 58 |
//Keep that event until we get a key up message |
sl@41 | 59 |
iHidEvents.Add(hidEvent); |
sl@41 | 60 |
} |
sl@41 | 61 |
|
sl@29 | 62 |
//Broadcast our events |
sl@29 | 63 |
OnHidEvent(this, hidEvent); |
sl@29 | 64 |
} |
sl@29 | 65 |
|
sl@41 | 66 |
public void OnHidEventRepeat(HidEvent aHidEvent) |
sl@41 | 67 |
{ |
sl@41 | 68 |
//Broadcast our events |
sl@41 | 69 |
OnHidEvent(this, aHidEvent); |
sl@41 | 70 |
} |
sl@41 | 71 |
|
sl@29 | 72 |
} |
sl@29 | 73 |
|
sl@29 | 74 |
} |