gpt4 book ai didi

c# - 从 USB PC Remote 读取输入

转载 作者:行者123 更新时间:2023-11-30 15:06:44 26 4
gpt4 key购买 nike

我的电脑有一个 Remote ,它通过 USB 连接。它不需要驱动程序,但不支持某些多媒体键。我想知道是否有一种方法可以读取输入数据以便我自己处理事件?

编辑:我找到了一个名为 USBlyzer 的软件,它可以显示我需要的所有信息,并且能够处理 usb 事件。问题是它是闭源的

编辑:这是同一个问题:https://superuser.com/questions/179457/software-to-customise-this-usb-pc-remote-control但仍然没有回答我。我有这个 Remote

最佳答案

我认为 RawInput 是您所需要的。调用 RegisterRawInputDevices(使用平台调用)订阅事件。然后覆盖主窗口的WndProc。并调用GetRawInputData 读取和解析原始数据。

例子:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace StackOverflow
{
static class Program
{
static void Main()
{
Application.Run(new Form1());
}
}

public static class KeyboardRawInput
{
public struct RawKeyboard
{
public int Type;
public int Size;
public IntPtr Device;
public IntPtr WParam;

public ushort MakeCode;
public ushort Flags;
public ushort Reserved;
public ushort VKey;
public uint Message;
public uint ExtraInformation;

}

public struct RawInputDevice
{
public ushort Page;
public ushort Usage;
public int Flags;
public IntPtr HWnd;
}


[DllImport("user32", SetLastError = true)]
public static extern bool RegisterRawInputDevices(
[MarshalAs(UnmanagedType.LPArray)] RawInputDevice[] devs,
uint count,
int structSize);

[DllImport("user32")]
public static extern uint GetRawInputData(
IntPtr hrawInput,
uint command,
ref RawKeyboard data,
ref uint size,
int headerSize);
}

class Form1 : Form
{
protected override void OnLoad(EventArgs e)
{
try {
KeyboardRawInput.RawInputDevice dev = new KeyboardRawInput.RawInputDevice();
dev.Page = 1;
dev.Usage = 6;
dev.Flags = 0x00000100 /*RIDEV_INPUTSINK*/;
dev.HWnd = this.Handle;

bool result = KeyboardRawInput.RegisterRawInputDevices(new KeyboardRawInput.RawInputDevice[] { dev }, 1, Marshal.SizeOf(typeof(KeyboardRawInput.RawInputDevice)));
if (!result)
throw new Exception(string.Format("LastError: 0x{0:x}", Marshal.GetLastWin32Error()));

} catch (Exception ex) {
MessageBox.Show(ex.Message, "Error registering RawInput");
}

base.OnLoad(e);
}

protected override void WndProc(ref Message m)
{
if (m.Msg == 0xFF) {
KeyboardRawInput.RawKeyboard keyboard = new KeyboardRawInput.RawKeyboard();
uint size = (uint)Marshal.SizeOf(keyboard);
uint result = KeyboardRawInput.GetRawInputData(m.LParam, 0x10000003, ref keyboard, ref size, 4 + 4 + IntPtr.Size * 2);
if (result != uint.MaxValue) {
string parse = string.Format("MakeCode: 0x{0:X}\r\nMessage: 0x{1:X}\r\nVKey: 0x{2:X}", keyboard.MakeCode, keyboard.Message, keyboard.VKey);
MessageBox.Show(parse);
}
}

base.WndProc(ref m);
}
}
}

关于c# - 从 USB PC Remote 读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7371293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com