所以我有一个控制台应用程序,我从user32.dll导入了RegisterRawInputDevices
您无法将设备注册到控制台窗口处理程序,因此我创建了一个继承自Form的类。这也是覆盖WndProc的类。现在我所做的就是将message.Msg写入控制台。
所以我实例化窗体并将窗口处理程序传递给RegisterRawInputDevices,它注册鼠标和键盘。但之后窗口不再收到任何消息 编辑:添加了一些代码以显示我目前正在尝试的内容 这是我的主要方法(虽然我有一些其他代码可以使窗口保持活动状态):
Input window = new Input();
PS4Input.RegisterControllers(window.Handle);
这是我用来创建窗口的类。漂亮的基础:
public class Input : Form
{
protected override void WndProc(ref Message message)
{
Console.WriteLine(message.Msg);
base.WndProc(ref message);
}
}
以下是我注册设备的方法。它目前说它有效。
[DllImport("User32.dll", SetLastError = true)]
extern static uint GetRawInputDeviceList(IntPtr pRawInputDeviceList, ref uint uiNumDevices, uint cbSize);
[DllImport("User32.dll", SetLastError = true)]
extern static uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, ref DeviceInfo pData, ref uint pcbSize);
[DllImport("user32.dll", SetLastError = true)]
static extern bool RegisterRawInputDevices(RawInputDevice[] pRawInputDevice, uint numberDevices, uint size);
[DllImport("user32.dll", SetLastError = true)]
internal extern static uint GetRegisteredRawInputDevices(RawInputDevice[] pRawInputDevice, ref uint puiNumDevices, uint cbSize);
public static void RegisterControllers(IntPtr hwnd)
{
uint deviceCount = 0;
int dwSize = Marshal.SizeOf(typeof(RawInputDeviceList));
if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
{
IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));
GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);
for (int i = 0; i < deviceCount; i++)
{
RawInputDeviceList rid = (RawInputDeviceList)Marshal.PtrToStructure(new IntPtr(pRawInputDeviceList.ToInt32() + (dwSize * i)), typeof(RawInputDeviceList));
uint size = (uint)Marshal.SizeOf(typeof(DeviceInfo));
var di = new DeviceInfo { Size = Marshal.SizeOf(typeof(DeviceInfo)) };
GetRawInputDeviceInfo(rid.hDevice, (uint)RawInputDeviceInfo.RIDI_DEVICEINFO, ref di, ref size);
if (di.Type == DeviceType.RimTypeHid && di.HIDInfo.Usage == (ushort)HidUsage.Gamepad && di.HIDInfo.UsagePage == (ushort)HidUsagePage.GENERIC)
{
var device = new RawInputDevice();
Console.WriteLine("Registering Device");
device.UsagePage = di.HIDInfo.UsagePage;
device.Usage = (ushort)HidUsage.Keyboard;
device.Flags = RawInputDeviceFlags.INPUTSINK;
device.Target = hwnd;
RawInputDevice[] devices = new RawInputDevice[1];
devices[0] = device;
if (RegisterRawInputDevices(devices, (uint)devices.Length, (uint)Marshal.SizeOf(typeof(RawInputDevice))) == false)
{
Console.WriteLine("Failure");
return;
}
else
{
Console.WriteLine("Success!");
}
break;
}
}
Marshal.FreeHGlobal(pRawInputDeviceList);
}
else
{
Console.WriteLine(Marshal.GetLastWin32Error());
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct RawInputDevice
{
internal ushort UsagePage;
internal ushort Usage;
internal RawInputDeviceFlags Flags;
internal IntPtr Target;
public override string ToString()
{
return string.Format("{0}/{1}, flags: {2}, target: {3}", UsagePage, Usage, Flags, Target);
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct RawInputDeviceList
{
public IntPtr hDevice;
public uint dwType;
}
[StructLayout(LayoutKind.Explicit)]
public struct DeviceInfo
{
[FieldOffset(0)]
public int Size;
[FieldOffset(4)]
public int Type;
[FieldOffset(8)]
public DeviceInfoMouse MouseInfo;
[FieldOffset(8)]
public DeviceInfoKeyboard KeyboardInfo;
[FieldOffset(8)]
public DeviceInfoHid HIDInfo;
public override string ToString()
{
return string.Format("DeviceInfo\n Size: {0}\n Type: {1}\n", Size, Type);
}
}
答案 0 :(得分:0)
没有足够的细节,但我认为你的代码看起来像这样:
public static void Main(string[] args)
{
MyClass messageReciever = new MyClass();
messageReciever.StartRecievingMessages();
Console.WriteLine("Press Q to exit...");
while (Console.ReadKey(true).Key != ConsoleKey.Q) { }
}
将[STAThread]添加到您的入口点以保持窗口活着:
[STAThread]
public static void Main(string[] args)
更新: 我有一个解决方案给你我的朋友!在程序输入中,您需要添加:
Application.Run(window);
并且在Input类中覆盖OnShow(在出现时隐藏窗口):
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
Hide();
}
原因是表格必须首先获得焦点,然后只有Flag - &#34;继续捕捉背景&#34;的工作原理。