Publishing on NuGet.
2 // Copyright (C) 2014-2015 Stéphane Lenclud.
4 // This file is part of SharpLibHid.
6 // SharpDisplayManager is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // SharpDisplayManager is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with SharpDisplayManager. If not, see <http://www.gnu.org/licenses/>.
22 using System.Windows.Forms;
23 using System.Runtime.InteropServices;
24 using System.Diagnostics;
26 using Microsoft.Win32.SafeHandles;
28 using System.Collections.Generic;
31 namespace SharpLib.Hid
34 /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
38 public delegate void HidEventHandler(object aSender, Event aHidEvent);
39 public event HidEventHandler OnHidEvent;
40 List<Event> iHidEvents;
43 public bool IsRegistered { get; private set; }
45 public Handler(RAWINPUTDEVICE[] aRawInputDevices)
47 iHidEvents=new List<Event>();
48 IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
52 /// Process a WM_INPUT message.
54 /// <param name="aMessage"></param>
55 public void ProcessInput(ref Message aMessage)
57 if (aMessage.Msg != Const.WM_INPUT)
59 //We only process WM_INPUT messages
63 Event hidEvent = new Event(aMessage, OnHidEventRepeat);
64 hidEvent.DebugWrite();
66 if (!hidEvent.IsValid || !hidEvent.IsGeneric)
68 Debug.WriteLine("Skipping HID message.");
73 if (hidEvent.IsButtonUp)
75 //This is a key up event
76 //We need to discard any events belonging to the same page and collection
77 for (int i = (iHidEvents.Count-1); i >= 0; i--)
79 if (iHidEvents[i].UsageId == hidEvent.UsageId)
81 iHidEvents[i].Dispose();
82 iHidEvents.RemoveAt(i);
88 //Keep that event until we get a key up message
89 iHidEvents.Add(hidEvent);
92 //Broadcast our events
93 OnHidEvent(this, hidEvent);
96 public void OnHidEventRepeat(Event aHidEvent)
98 //Broadcast our events
99 OnHidEvent(this, aHidEvent);