/// <summary> /// Register raw input device as defined and imported from our XML. /// </summary> /// <param name="aHWND"></param> public void Register(IntPtr aHWND) { SharpLib.Win32.RAWINPUTDEVICE[] rid = new SharpLib.Win32.RAWINPUTDEVICE[_usageActions.Count]; var i = 0; foreach (var entry in _usageActions) { rid[i].usUsagePage = entry.Value.UsagePage; rid[i].usUsage = entry.Value.UsageCollection; rid[i].dwFlags = (entry.Value.HandleHidEventsWhileInBackground ? SharpLib.Win32.RawInputDeviceFlags.RIDEV_INPUTSINK : 0); //Funny enough that "no legacy" flag prevents our HID keyboard handler to work //rid[i].dwFlags |= SharpLib.Win32.RawInputDeviceFlags.RIDEV_NOLEGACY; rid[i].hwndTarget = aHWND; i++; } int repeatDelay = -1; int repeatSpeed = -1; using (Settings settings = new MPSettings()) { repeatDelay = settings.GetValueAsInt("remote", "HidRepeatDelayInMs", repeatDelay); repeatSpeed = settings.GetValueAsInt("remote", "HidRepeatSpeedInMs", repeatSpeed); } _handler = new SharpLib.Hid.Handler(rid, true, repeatDelay, repeatSpeed); if (!_handler.IsRegistered) { Log.Info("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } _handler.OnHidEvent += OnHidEvent; }
public void RegisterHidDevices() { SharpLib.Win32.RAWINPUTDEVICE[] rid = new SharpLib.Win32.RAWINPUTDEVICE[1]; rid[0].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; rid[0].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; rid[0].dwFlags = RawInputDeviceFlags.RIDEV_EXINPUTSINK; rid[0].hwndTarget = Handle; int repeatDelay = -1; int repeatSpeed = -1; using (Settings settings = new MPSettings()) { repeatDelay = settings.GetValueAsInt("remote", "HidRepeatDelayInMs", repeatDelay); repeatSpeed = settings.GetValueAsInt("remote", "HidRepeatSpeedInMs", repeatSpeed); } _handler = new SharpLib.Hid.Handler(rid, true, repeatDelay, repeatSpeed); if (!_handler.IsRegistered) { Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } _handler.OnHidEvent += HandleHidEventThreadSafe; }
void CreateHandler() { int i = 0; RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1]; rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.VirtualRealityControls; rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.VirtualReality.HeadTracker; rid[i].dwFlags = RawInputDeviceFlags.RIDEV_EXINPUTSINK; rid[i].hwndTarget = Handle; //i++; //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard; //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; //rid[i].hwndTarget = Handle; //i++; //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls; //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse; //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; //rid[i].hwndTarget = aHWND; iHandler = new SharpLib.Hid.Handler(rid); if (!iHandler.IsRegistered) { Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } iHandler.OnHidEvent += OnHidEvent; }
private void DisposeHandlers() { if (iHidHandler != null) { //First de-register iHidHandler.Dispose(); iHidHandler = null; } if (iHidParser != null) { //First de-register iHidParser.Dispose(); iHidParser = null; } }
public HeadPhone() { InitializeComponent(); SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler((sender, e) => SystemEvents_PowerModeChanged(sender, e, Token)); SystemEvents.SessionSwitch += new SessionSwitchEventHandler((sender, e) => SystemEvents_SessionSwitch(sender, e, Token)); Trace.WriteLine("Initialized"); foreach (string Arg in Environment.GetCommandLineArgs()) { if (Arg.ToLower().StartsWith("-username="******"-username="******"-password="******"-password="******"-host=")) { host = Arg.Remove(0, "-host=".Length); } } if (host.Length == 0) { MessageBox.Show("HeadPhone not running"); System.Environment.Exit(1); } Trace.WriteLine("Read Parms.."); RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1]; rid[0].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Telephony; rid[0].usUsage = (ushort)SharpLib.Hid.UsageCollection.Telephony.Headset; rid[0].dwFlags = (RawInputDeviceFlags)256; rid[0].hwndTarget = Handle; Trace.WriteLine("Rid defined.."); iHidHandler = new SharpLib.Hid.Handler(rid, false, -1, -1); iHidParser = iHidHandler; iHidParser.OnHidEvent += HandleHidEventThreadSafe; Trace.WriteLine("Handle Hid"); }
public void Dispose() { if (_hidThreadId != 0) { NativeMethods.PostThreadMessage(_hidThreadId, NativeMethods.WM_QUIT, IntPtr.Zero, IntPtr.Zero); _hidEventThread.Join(); _hidThreadId = 0; _hidEventThread = null; } if (_hidHandler != null) { //First de-register _hidHandler.Dispose(); _hidHandler = null; } if (_dummyWindow != IntPtr.Zero) { NativeMethods.DestroyWindow(_dummyWindow); } }
/// <summary> /// Register HID devices so that we receive corresponding WM_INPUT messages. /// </summary> protected void RegisterHidDevices(IntPtr aHwnd) { // Register the input device to receive the commands from the // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp // for the vendor defined usage page. RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1]; int i = 0; rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; rid[i].dwFlags = Const.RIDEV_INPUTSINK; rid[i].hwndTarget = aHwnd; //Process.GetCurrentProcess().MainWindowHandle; _hidHandler = new SharpLib.Hid.Handler(rid); if (!_hidHandler.IsRegistered) { Log.Write("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } _hidHandler.OnHidEvent += HandleHidEvent; }
private void HidEventHandlerThread() { Thread.BeginThreadAffinity(); try { _hidThreadId = NativeMethods.GetCurrentThreadId(); NativeMethods.WindowClass wndclass; wndclass.style = 0; wndclass.lpfnWndProc = HidEventThreadWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = Process.GetCurrentProcess().Handle; wndclass.hIcon = IntPtr.Zero; wndclass.hCursor = IntPtr.Zero; wndclass.hbrBackground = IntPtr.Zero; wndclass.lpszMenuName = null; wndclass.lpszClassName = "HidEventHandlerThreadWndClass"; NativeMethods.RegisterClass(ref wndclass); _dummyWindow = NativeMethods.CreateWindowEx(0x80, wndclass.lpszClassName, "", 0x80000000, 0, 0, 0, 0, HWND_MESSAGE, IntPtr.Zero, wndclass.hInstance, IntPtr.Zero); if (_dummyWindow.Equals(IntPtr.Zero)) { ServiceRegistration.Get <ILogger>().Debug("HidEventHandlerThread cannot create window handle, exiting thread"); return; } SharpLib.Win32.RawInputDeviceFlags flags = SharpLib.Win32.RawInputDeviceFlags.RIDEV_INPUTSINK; IntPtr handle = _dummyWindow; List <RAWINPUTDEVICE> devices = new List <RAWINPUTDEVICE>(); devices.Add(new RAWINPUTDEVICE { usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl, usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl, dwFlags = flags, hwndTarget = handle }); devices.Add(new RAWINPUTDEVICE { usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer, usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl, dwFlags = flags, hwndTarget = handle }); devices.Add(new RAWINPUTDEVICE { usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls, usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard, dwFlags = flags, hwndTarget = handle }); _hidHandler = new SharpLib.Hid.Handler(devices.ToArray(), true, -1, -1); _hidHandler.OnHidEvent += new Handler.HidEventHandler(OnHidEvent); // This thread needs a message loop to handle power messages from Windows. ServiceRegistration.Get <ILogger>().Debug("HidEventHandlerThread message loop is running"); while (true) { try { NativeMethods.Message msgApi = new NativeMethods.Message(); if (!NativeMethods.GetMessageA(ref msgApi, IntPtr.Zero, 0, 0)) // returns false on WM_QUIT { return; } NativeMethods.TranslateMessage(ref msgApi); NativeMethods.DispatchMessageA(ref msgApi); } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Error("HidEventHandlerThread", ex); } } } finally { Thread.EndThreadAffinity(); ServiceRegistration.Get <ILogger>().Debug("HidEventHandlerThread finished"); } }
/// <summary> /// Go through our device list and register the devices that are checked. /// </summary> void RegisterHidDevices() { // Register the input device to receive the commands from the // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp // for the vendor defined usage page. DisposeHandlers(); //Get our flags RadioButton checkedRadioButton = groupBoxRegistrationFlag.Controls.OfType <RadioButton>().FirstOrDefault(r => r.Checked); uint flags = uint.Parse((string)checkedRadioButton.Tag, System.Globalization.NumberStyles.HexNumber); // See: Const.RIDEV_EXINPUTSINK and Const.RIDEV_INPUTSINK //We collect our devices in a dictionary to remove duplicates Dictionary <uint, Hid.Device> devices = new Dictionary <uint, Hid.Device>(); foreach (TreeNode node in treeViewDevices.Nodes) { Hid.Device device = (Hid.Device)node.Tag; //Now allowing mouse and keyboard to register too if (node.Checked /*&& device.IsHid*/) { try { devices.Add(device.UsageId, device); } catch //(Exception ex) { //Just ignore duplicate UsageId Debug.WriteLine("Similar device already registered!"); } } } if (devices.Count == 0) { //No device to register for, nothing to do here return; } int i = 0; RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[devices.Count]; foreach (KeyValuePair <uint, Hid.Device> entry in devices) { rid[i].usUsagePage = entry.Value.UsagePage; rid[i].usUsage = entry.Value.UsageCollection; rid[i].dwFlags = (RawInputDeviceFlags)flags; rid[i].hwndTarget = Handle; i++; } /* * int i = 0; * rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; * rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; * rid[i].dwFlags = flags; * rid[i].hwndTarget = Handle; * * i++; * rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; * rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterLowLevel; * rid[i].dwFlags = flags; * rid[i].hwndTarget = Handle; * * i++; * rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer; * rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl; * rid[i].dwFlags = flags; * rid[i].hwndTarget = Handle; * * i++; * rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer; * rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection; * rid[i].dwFlags = flags; * rid[i].hwndTarget = Handle; * * i++; * rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; * rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl; * rid[i].dwFlags = flags; * rid[i].hwndTarget = Handle; * * i++; * rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; * rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad; * rid[i].dwFlags = flags; * rid[i].hwndTarget = Handle; */ //i++; //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard; //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; //rid[i].hwndTarget = Handle; //i++; //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls; //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse; //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; //rid[i].hwndTarget = aHWND; iHidHandler = new SharpLib.Hid.Handler(rid, checkBoxRepeat.Checked, (int)numericRepeatDelay.Value, (int)numericRepeatSpeed.Value); if (!iHidHandler.IsRegistered) { Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } if (checkBoxUseSingleHandler.Checked) { iHidParser = iHidHandler; } else { //For testing purposes we parse WM_INPUT messages from another Handler instance. iHidParser = new SharpLib.Hid.Handler(checkBoxRepeat.Checked, (int)numericRepeatDelay.Value, (int)numericRepeatSpeed.Value); } iHidParser.OnHidEvent += HandleHidEventThreadSafe; }
/// <summary> /// Register HID devices so that we receive corresponding WM_INPUT messages. /// </summary> protected void RegisterHidDevices(IntPtr aHwnd) { // Register the input device to receive the commands from the // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp // for the vendor defined usage page. RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1]; int i = 0; rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK; rid[i].hwndTarget = aHwnd; //Process.GetCurrentProcess().MainWindowHandle; _hidHandler = new SharpLib.Hid.Handler(rid); if (!_hidHandler.IsRegistered) { Log.Write("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } _hidHandler.OnHidEvent += HandleHidEvent; }
public void RegisterHidDevices() { SharpLib.Win32.RAWINPUTDEVICE[] rid = new SharpLib.Win32.RAWINPUTDEVICE[1]; rid[0].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; rid[0].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; rid[0].dwFlags = Const.RIDEV_EXINPUTSINK; rid[0].hwndTarget = Handle; int repeatDelay = -1; int repeatSpeed = -1; using (Settings settings = new MPSettings()) { repeatDelay = settings.GetValueAsInt("remote", "HidRepeatDelayInMs", repeatDelay); repeatSpeed = settings.GetValueAsInt("remote", "HidRepeatSpeedInMs", repeatSpeed); } _handler = new SharpLib.Hid.Handler(rid, true, repeatDelay, repeatSpeed); if (!_handler.IsRegistered) { Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } _handler.OnHidEvent += HandleHidEventThreadSafe; }