public void OnHidEventRepeat(HidEvent aHidEvent) { //Broadcast our events //OnHidEvent(this, aHidEvent); if (aHidEvent.IsStray) //Defensive { return; } HidListener.LogInfo("HID: Repeat"); HidUsageAction usageAction; if (_usageActions.TryGetValue(aHidEvent.UsageId, out usageAction)) { //Alright we do handle this usage ID //Try mapping actions to each of our usage foreach (var usage in aHidEvent.Usages) { HidListener.LogInfo("HID: try mapping usage {0}", usage.ToString("X4")); usageAction.MapAction(usage, aHidEvent.IsBackground, true); } } }
public List <MediaPortal.InputDevices.HidUsageAction.ConditionalAction> ProcessInput(Message aMessage, bool shouldRaiseAction = true) { var result = new List <MediaPortal.InputDevices.HidUsageAction.ConditionalAction>(); var hidEvent = new HidEvent(aMessage, OnHidEventRepeat); if (HidListener.Verbose) //Avoid string conversion if not needed { Log.Info("\n" + hidEvent.ToString()); } if (!hidEvent.IsValid || !hidEvent.IsGeneric) { HidListener.LogInfo("HID: Skipping HID message."); return(null); } // if (hidEvent.Usages[0] == 0) { //This is a key up event //We need to discard any events belonging to the same page and collection for (var i = (_hidEvents.Count - 1); i >= 0; i--) { if (_hidEvents[i].UsageId == hidEvent.UsageId) { _hidEvents[i].Dispose(); _hidEvents.RemoveAt(i); } } } else { //Keep that event until we get a key up message _hidEvents.Add(hidEvent); } //Broadcast our events //OnHidEvent(this, hidEvent); HidUsageAction usageAction; if (_usageActions.TryGetValue(hidEvent.UsageId, out usageAction)) { //Alright we do handle this usage ID //Try mapping actions to each of our usage foreach (ushort usage in hidEvent.Usages) { HidListener.LogInfo("HID: try mapping usage {0}", usage.ToString("X4")); result.Add(usageAction.GetAction(usage.ToString(), hidEvent.IsBackground, false)); if (shouldRaiseAction) { usageAction.MapAction(usage, hidEvent.IsBackground, false); } } return(result); } return(null); }
public HID(IntPtr windowHandle) { if (!Win32Bindings.user32.Window.IsWindow(windowHandle)) { throw new ArgumentException("windowHandle is invalid"); } _handle = windowHandle; _source = HwndSource.FromHwnd(_handle); if (_source == null) { throw new Exception("Can nnot create HwndSource from windowHandle"); } _source.AddHook(WindowsMessageHandler); HidEventInternal += x => { HidEvent?.Invoke(x); }; }
private void OnHidDataReceived(object sender, HidEvent e) { try { if (this.startButton.IsEnabled) { return; } var currentDevice = this.listView.SelectedItem as IIinputDevice; if (currentDevice == null) { return; } if (e.Device == null) { if (e.IsKeyboard) { // On-Screen Keyboard var log = string.Format( "{0} {1} | Make code: {2} | VKey: {3}", e.VirtualKey, e.IsButtonDown ? "[pressed]" : "[released]", e.RawInput.keyboard.MakeCode, e.RawInput.keyboard.VKey); this.rtb.Document.Blocks.Add(new Paragraph(new Run(log))); } return; } if (currentDevice is UsbGamepad && e.InputReport == null) { return; } var senderVid = e.Device.VendorId.ToString("X4"); var senderPid = e.Device.ProductId.ToString("X4"); if (currentDevice.Vendor != senderVid || currentDevice.Product != senderPid) { return; } var senderTokens = e.Device.Name.Split(new char[] { '#' }); if (senderTokens.Length != 4) { return; } var senderDeviceID = senderTokens[1].ToLower(); var currentTokens = currentDevice.DeviceID.Split(new char[] { '\\' }); var currDeviceID = currentTokens[1].ToLower(); if (senderDeviceID != currDeviceID) { return; } if (e.InputReport != null) { if (this.dataCheckBoxes == null || this.dataCheckBoxes.Count != e.InputReport.Length) { this.dataCheckBoxes = new List <CheckBox>(); this.lastData = new byte[e.InputReport.Length]; this.layersWrapPanel.Children.Clear(); for (int i = 0; i < e.InputReport.Length; i++) { var checkbox = new CheckBox() { Content = i, Margin = new Thickness(5), IsChecked = true, ToolTip = "Subscribe for byte " + i + " changes" }; this.dataCheckBoxes.Add(checkbox); this.layersWrapPanel.Children.Add(checkbox); } } } var currentText = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text; if (this.GetLines(currentText).Length > 100) { this.rtb.Document.Blocks.Clear(); this.rtb.Document.Blocks.Add(new Paragraph(new Run("--- AutoCleared (exeed 100 lines) ---"))); } var paragraph = new Paragraph(); if (currentDevice is UsbGamepad) { paragraph = this.CreateColorParagraphFromData(e.InputReport, this.lastData); this.lastData = e.InputReport; } else if (currentDevice is HidKeyboard) { paragraph.Inlines.Add(new Run(string.Format("{0} {1} | Make code: {2} | VKey: {3}", e.VirtualKey, e.IsButtonDown ? "[pressed]" : "[released]", e.RawInput.keyboard.MakeCode, e.RawInput.keyboard.VKey))); } else if (currentDevice is HidMouse) { // Getting the mouse buttons flags var mouseButtonFlags = e.RawInput.mouse.buttonsStr.usButtonFlags; if (mouseButtonFlags != 0) { paragraph.Inlines.Add(new Run(mouseButtonFlags.ToString())); } else { // Getting the mouse delta move coordinates paragraph.Inlines.Add(new Run(string.Format("Mouse delta move X:{0} Y:{1}", e.RawInput.mouse.lLastX, e.RawInput.mouse.lLastY))); } } else { // Other device if (e.InputReport != null) { paragraph = this.CreateColorParagraphFromData(e.InputReport, this.lastData); this.lastData = e.InputReport; } else { // Displaying the unknown data paragraph.Inlines.Add(new Run(string.Format("Unknown raw data received:{0}{1}", Environment.NewLine, e.RawInput))); paragraph.Foreground = Brushes.Red; this.rtb.Document.Blocks.Add(paragraph); return; } } if (paragraph.Inlines.Count == 0) { return; } var lastLine = this.GetLines(currentText).LastOrDefault(); var newLine = string.Join(string.Empty, paragraph.Inlines.Select(line => line.ContentStart.GetTextInRun(LogicalDirection.Forward))); if (lastLine != newLine) { this.rtb.Document.Blocks.Add(paragraph); } } catch (Exception ex) { this.rtb.Document.Blocks.Add(new Paragraph(new Run("Error: " + ex.Message) { Foreground = Brushes.Red, Background = Brushes.Black })); } }
private void OnHidDataReceived(object sender, HidEvent e) { try { if (this.startButton.IsEnabled) { return; } var currentDevice = this.listView.SelectedItem as Device; if (currentDevice == null) { return; } if (e.Device == null) { if (e.IsKeyboard) { // On-Screen Keyboard var log = string.Format( "{0} {1} | Make code: {2} | VKey: {3}", e.VirtualKey, e.IsButtonDown ? "[pressed]" : "[released]", e.RawInput.keyboard.MakeCode, e.RawInput.keyboard.VKey); this.rtb.Document.Blocks.Add(new Paragraph(new Run(log))); } return; } if (currentDevice.IsGamePad() && e.InputReport == null) { return; } if (currentDevice.Name != e.Device.Name) { return; } if (e.InputReport != null) { if (this.dataCheckBoxes == null || this.dataCheckBoxes.Count != e.InputReport.Length) { this.dataCheckBoxes = new List <CheckBox>(); this.lastData = new byte[e.InputReport.Length]; this.layersWrapPanel.Children.Clear(); for (int i = 0; i < e.InputReport.Length; i++) { var checkbox = new CheckBox() { Content = i, Margin = new Thickness(5), IsChecked = true, ToolTip = "Subscribe for byte " + i + " changes" }; this.dataCheckBoxes.Add(checkbox); this.layersWrapPanel.Children.Add(checkbox); } } } var currentText = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text; if (this.GetLines(currentText).Length > 100) { this.rtb.Document.Blocks.Clear(); this.rtb.Document.Blocks.Add(new Paragraph(new Run("--- AutoCleared (exeed 100 lines) ---"))); } var paragraph = new Paragraph(); if (currentDevice.IsGamePad()) { paragraph = this.CreateColorParagraphFromData(e.InputReport, this.lastData); this.lastData = e.InputReport; } else if (currentDevice.IsKeyboard) { paragraph.Inlines.Add(new Run(string.Format("{0} {1} | Make code: {2} | VKey: {3}", e.VirtualKey, e.IsButtonDown ? "[pressed]" : "[released]", e.RawInput.keyboard.MakeCode, e.RawInput.keyboard.VKey))); } else if (currentDevice.IsMouse) { // Getting the mouse buttons flags var mouseButtonFlags = e.RawInput.mouse.buttonsStr.usButtonFlags; if (mouseButtonFlags != 0) { paragraph.Inlines.Add(new Run(mouseButtonFlags.ToString())); } else { // Getting the mouse delta move coordinates paragraph.Inlines.Add(new Run(string.Format("Mouse delta move X:{0} Y:{1}", e.RawInput.mouse.lLastX, e.RawInput.mouse.lLastY))); } } else { if (currentDevice.IsStylus() || currentDevice.IsFinger()) { foreach (var c in e.Device.InputValueCapabilities) { Debug.Assert(e.UsageValues.ContainsKey(c)); // must be contained in dictionary if ((c.UsagePage & 0xFF00) == 0xFF00) { continue; // skip Vendor-defined usage } Debug.WriteLine(""); if (c.HasLink()) { Debug.Write($"{c.GetLinkName()} | "); } Debug.Write($"{c.GetName()} : {e.UsageValues[c]}"); // cap is key of dictionary if (c.HasUnit()) { Debug.Write($" = {c.ConvertUnit(e.UsageValues[c])} [{c.GetUnit()}]"); } } foreach (var c in e.Device.InputButtonCapabilities) { Debug.Assert(e.Buttons.ContainsKey(c)); // must be contained in dictionary Debug.WriteLine(""); if (c.HasLink()) { Debug.Write($"{c.GetLinkName()} | "); } var btn = e.Buttons[c] ? "on": "off"; Debug.Write($"{c.GetName()} : {btn}"); } Debug.WriteLine(""); } else // Other device if (e.InputReport != null) { paragraph = this.CreateColorParagraphFromData(e.InputReport, this.lastData); this.lastData = e.InputReport; } else { // Displaying the unknown data paragraph.Inlines.Add(new Run(string.Format("Unknown raw data received:{0}{1}", Environment.NewLine, e.RawInput))); paragraph.Foreground = Brushes.Red; this.rtb.Document.Blocks.Add(paragraph); return; } } if (paragraph.Inlines.Count == 0) { return; } var lastLine = this.GetLines(currentText).LastOrDefault(); var newLine = string.Join(string.Empty, paragraph.Inlines.Select(line => line.ContentStart.GetTextInRun(LogicalDirection.Forward))); if (lastLine != newLine) { this.rtb.Document.Blocks.Add(paragraph); } } catch (Exception ex) { this.rtb.Document.Blocks.Add(new Paragraph(new Run("Error: " + ex.Message) { Foreground = Brushes.Red, Background = Brushes.Black })); } }