StephaneLenclud@76: // StephaneLenclud@76: // Copyright (C) 2014-2015 Stéphane Lenclud. StephaneLenclud@76: // StephaneLenclud@76: // This file is part of SharpLibHid. StephaneLenclud@76: // StephaneLenclud@76: // SharpDisplayManager is free software: you can redistribute it and/or modify StephaneLenclud@76: // it under the terms of the GNU General Public License as published by StephaneLenclud@76: // the Free Software Foundation, either version 3 of the License, or StephaneLenclud@76: // (at your option) any later version. StephaneLenclud@76: // StephaneLenclud@76: // SharpDisplayManager is distributed in the hope that it will be useful, StephaneLenclud@76: // but WITHOUT ANY WARRANTY; without even the implied warranty of StephaneLenclud@76: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the StephaneLenclud@76: // GNU General Public License for more details. StephaneLenclud@76: // StephaneLenclud@76: // You should have received a copy of the GNU General Public License StephaneLenclud@76: // along with SharpDisplayManager. If not, see . StephaneLenclud@76: // StephaneLenclud@76: sl@0: using System; sl@0: using System.Drawing; sl@0: using System.Collections; sl@0: using System.ComponentModel; sl@0: using System.Windows.Forms; sl@0: using System.Data; StephaneLenclud@78: using System.Diagnostics; StephaneLenclud@78: using System.Runtime.InteropServices; StephaneLenclud@81: using Hid = SharpLib.Hid; StephaneLenclud@78: using SharpLib.Win32; sl@0: StephaneLenclud@78: namespace HidDemo sl@0: { sl@0: /// StephaneLenclud@81: /// MainForm for our HID demo. sl@0: /// StephaneLenclud@60: public partial class MainForm : System.Windows.Forms.Form sl@0: { StephaneLenclud@81: private Hid.Handler iHidHandler; sl@0: StephaneLenclud@81: public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent); sl@41: sl@36: public MainForm() sl@0: { sl@0: // Required for Windows Form Designer support StephaneLenclud@78: InitializeComponent(); sl@0: } sl@0: sl@0: sl@0: /// sl@0: /// The main entry point for the application. sl@0: /// sl@0: [STAThread] sl@6: static void Main() sl@0: { StephaneLenclud@60: Application.EnableVisualStyles(); sl@36: Application.Run(new MainForm()); sl@0: } sl@0: StephaneLenclud@60: private void MainForm_Load(object sender, System.EventArgs e) StephaneLenclud@78: { StephaneLenclud@78: RegisterHidDevices(); StephaneLenclud@78: //Create our list of HID devices StephaneLenclud@77: SharpLib.Win32.RawInput.PopulateDeviceList(treeViewDevices); StephaneLenclud@78: } StephaneLenclud@60: StephaneLenclud@78: void RegisterHidDevices() StephaneLenclud@78: { StephaneLenclud@78: // Register the input device to receive the commands from the StephaneLenclud@78: // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp StephaneLenclud@78: // for the vendor defined usage page. StephaneLenclud@78: StephaneLenclud@83: if (iHidHandler!=null) StephaneLenclud@83: { StephaneLenclud@83: //First de-register StephaneLenclud@83: iHidHandler.Dispose(); StephaneLenclud@83: iHidHandler = null; StephaneLenclud@83: } StephaneLenclud@83: StephaneLenclud@83: StephaneLenclud@82: RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5]; StephaneLenclud@78: StephaneLenclud@78: int i = 0; StephaneLenclud@78: rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; StephaneLenclud@78: rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; StephaneLenclud@78: rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@78: rid[i].hwndTarget = Handle; StephaneLenclud@78: StephaneLenclud@78: i++; StephaneLenclud@78: rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer; StephaneLenclud@78: rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl; StephaneLenclud@78: rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@78: rid[i].hwndTarget = Handle; StephaneLenclud@78: StephaneLenclud@78: i++; StephaneLenclud@78: rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer; StephaneLenclud@78: rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection; StephaneLenclud@78: rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@78: rid[i].hwndTarget = Handle; StephaneLenclud@78: StephaneLenclud@78: i++; StephaneLenclud@78: rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; StephaneLenclud@78: rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl; StephaneLenclud@78: rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@78: rid[i].hwndTarget = Handle; StephaneLenclud@78: StephaneLenclud@78: i++; StephaneLenclud@78: rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; StephaneLenclud@78: rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad; StephaneLenclud@78: rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@78: rid[i].hwndTarget = Handle; StephaneLenclud@78: StephaneLenclud@82: //i++; StephaneLenclud@82: //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; StephaneLenclud@82: //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard; StephaneLenclud@78: //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@82: //rid[i].hwndTarget = Handle; StephaneLenclud@78: StephaneLenclud@78: //i++; StephaneLenclud@78: //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls; StephaneLenclud@78: //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse; StephaneLenclud@78: //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; StephaneLenclud@78: //rid[i].hwndTarget = aHWND; StephaneLenclud@78: StephaneLenclud@78: StephaneLenclud@83: iHidHandler = new SharpLib.Hid.Handler(rid, checkBoxRepeat.Checked); StephaneLenclud@78: if (!iHidHandler.IsRegistered) StephaneLenclud@78: { StephaneLenclud@78: Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); StephaneLenclud@78: } StephaneLenclud@78: iHidHandler.OnHidEvent += HandleHidEventThreadSafe; StephaneLenclud@78: } sl@0: StephaneLenclud@81: public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) sl@40: { sl@42: if (aHidEvent.IsStray) sl@42: { sl@42: //Stray event just ignore it sl@42: return; sl@42: } sl@42: sl@41: if (this.InvokeRequired) sl@41: { sl@41: //Not in the proper thread, invoke ourselves sl@41: OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); sl@41: this.Invoke(d, new object[] { aSender, aHidEvent }); sl@41: } sl@41: else sl@41: { sl@41: //We are in the proper thread sl@42: listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem()); StephaneLenclud@67: toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName; sl@41: } sl@40: } sl@0: sl@0: protected override void WndProc(ref Message message) sl@0: { StephaneLenclud@78: switch (message.Msg) sl@15: { StephaneLenclud@78: case Const.WM_KEYDOWN: StephaneLenclud@78: //ProcessKeyDown(message.WParam); StephaneLenclud@78: break; StephaneLenclud@78: case Const.WM_INPUT: StephaneLenclud@78: //Returning zero means we processed that message. StephaneLenclud@78: message.Result = new IntPtr(0); StephaneLenclud@78: iHidHandler.ProcessInput(ref message); StephaneLenclud@78: break; sl@15: } StephaneLenclud@78: //Is that needed? Check the docs. sl@0: base.WndProc(ref message); sl@0: } sl@0: StephaneLenclud@49: private void buttonClear_Click(object sender, EventArgs e) StephaneLenclud@49: { StephaneLenclud@49: listViewEvents.Items.Clear(); StephaneLenclud@49: } StephaneLenclud@49: StephaneLenclud@65: private void buttonTreeViewCollapseAll_Click(object sender, EventArgs e) StephaneLenclud@65: { StephaneLenclud@65: treeViewDevices.CollapseAll(); StephaneLenclud@65: } StephaneLenclud@65: StephaneLenclud@65: private void buttonTreeViewExpandAll_Click(object sender, EventArgs e) StephaneLenclud@65: { StephaneLenclud@65: treeViewDevices.ExpandAll(); StephaneLenclud@65: } StephaneLenclud@65: StephaneLenclud@72: private void buttonRefresh_Click(object sender, EventArgs e) StephaneLenclud@72: { StephaneLenclud@72: treeViewDevices.Nodes.Clear(); StephaneLenclud@77: SharpLib.Win32.RawInput.PopulateDeviceList(treeViewDevices); StephaneLenclud@72: } StephaneLenclud@72: StephaneLenclud@83: private void checkBoxRepeat_CheckedChanged(object sender, EventArgs e) StephaneLenclud@83: { StephaneLenclud@83: RegisterHidDevices(); StephaneLenclud@83: } StephaneLenclud@83: sl@0: } sl@0: }