我目前正在使用我的条形码扫描器应用程序,现在我已经陷入了RegisterRawInputDevices
功能。
Marshal.GetLastWin32Error()
总是抛出错误0.我无法解释原因。
我已经获得了当前控制台窗口的句柄,并且正确设置了属性(用法页面,用法,标志)。
因此它应该能够注册我的窗口可以收听的rawinput-device。 我正在浏览article written by Emma Burrows and Steve Messer at codeplex的示例代码。
The Method" InputDevice"是WPF示例代码的InputDevice类的构造函数。
/// <summary>
/// InputDevice constructor; registers the raw input devices
/// for the calling window.
/// </summary>
/// <param name="hwnd">Handle of the window listening for key presses</param>
public InputDevice(IntPtr hwnd)
{
RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];
rid[0].usUsagePage = 0x01; // -> set to hid
rid[0].usUsage = 0x06; // -> set to keyboard
rid[0].dwFlags = RIDEV_INPUTSINK; // -> set flag to let the window recieve
WM-Messages even if it is not focused
rid[0].hwndTarget = hwnd; // -> the handle of the window
if (!RegisterRawInputDevices(rid, (uint)rid.Length,
(uint)Marshal.SizeOf(rid[0])))
{
throw new ApplicationException (Marshal.GetLastWin32Error().ToString());
//throw new ApplicationException("Failed to register raw input device(s).");
}
}
在我的应用程序的Program.cs中调用StartWndProcHandler()
函数,函数本身获取窗口的处理程序,并实例为InputDevice
的对象。
// Gets the handler of the window and then instances a new inputdevice object
static void StartWndProcHandler()
{
IntPtr hwnd = IntPtr.Zero;
IntPtr myWin = GetConsoleWindow();
try
{
hwnd = myWin;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
device = new InputDevice(hwnd); // instances a new inputdevice
//NumberOfKeyboards = device.EnumerateDevices();
//device.KeyPressed += device_KeyPressed;
}
我确信我已了解RawInput注册的功能,并且我已经研究了源代码以了解其功能。
但是现在我的智慧结束了......对我来说,为什么应用程序无法注册任何设备似乎不合逻辑。
我希望有人能给我一个提示或方法。
更新
我能够提取详细的错误代码。这是错误1008,这意味着我想引用一个不存在的令牌。所以解决方案更接近一步。 :)
更新(01-12-2014)2:
Hello Guys,
经过几个小时的调试和搜索问题,我真的无法找到解决方案。
所有需要的值都传递给RawInputDevice
Struct,但我仍然得到错误1008。
我已经测试了上述文章的原始WPF Demoproject。它可以毫无问题地注册设备,但无法从键盘捕获原始输入。 因此我猜测设备的注册应该可行。
所以我希望有人能够给我任何建议或帮助。
祝你好运
更新(2014年12月23日):
大家好,
我们解决了完全不同的问题。因为我们使用了连接scangun的debian机器,所以我们现在能够使用bashscript轻松读出scangun输入。 问题仍然没有解决,但我们现在不再关注。祝你好运