USB 条码扫描器和 WM_KEYDOWN
我正在尝试编写一个可以读取条形码扫描仪的程序。此外,即使应用程序不是焦点窗口(即在系统托盘中运行等),我也需要它来读取输入。
我找到了这篇文章,标题为 在WinForms中区分条形码扫描仪和键盘,这似乎解决了确切的问题。它工作得很好,它检测我的设备并处理 WM_INPUT 消息。
但是,它正在检查 RAWINPUT.keyboard.Message 是否为 WM_KEYDOWN (0x100)。它似乎从来没有收到过这个。我在 文章 正在添加一个 Console.Out.WriteLine 来输出该消息的实际值:
Console.Out.WriteLine("message: {0}", raw.keyboard.Message.ToString("X"));
if (raw.keyboard.Message == NativeMethods.WM_KEYDOWN)
{
....
这是它输出的内容:
message: B
message: 1000B
message: 3
message: 10003
message: 8
message: 10008
message: 3
message: 10003
message: 5
message: 10005
message: 3
message: 10003
message: 8
message: 10008
message: 8
message: 10008
message: 4
message: 10004
message: 9
message: 10009
message: 9
message: 10009
message: 3
message: 10003
当正确完成时我期望收到的值是:
257232709
我通过扫描到记事本进行验证。
我不知道操作系统是否与这里相关,但我想我应该提到我在 Windows 7 64 和 Visual Studio 2010 以及 .NET Framework 3.5 中运行它。扫描仪是 USB 条码扫描仪,Symbol LS2208,设置为“HID KEYBOARD EMULATION”
I am trying to write a program that can will read a barcode scanner. In addition, I need it to read the input even when the application is not the window in focus (i.e., running in system tray, etc).
I found this article, titled Distinguishing Barcode Scanners from the Keyboard in WinForms, that seems to solve the exact problem. It is working pretty good, it detects my device and handles the WM_INPUT message.
However, it is checking to see if the RAWINPUT.keyboard.Message is WM_KEYDOWN (0x100). It never seems to receive this. The only line of code I've altered in the code provided in the article is adding a Console.Out.WriteLine to output the actual values of that message:
Console.Out.WriteLine("message: {0}", raw.keyboard.Message.ToString("X"));
if (raw.keyboard.Message == NativeMethods.WM_KEYDOWN)
{
....
Here is what it outputs:
message: B
message: 1000B
message: 3
message: 10003
message: 8
message: 10008
message: 3
message: 10003
message: 5
message: 10005
message: 3
message: 10003
message: 8
message: 10008
message: 8
message: 10008
message: 4
message: 10004
message: 9
message: 10009
message: 9
message: 10009
message: 3
message: 10003
The value I'm expecting to receive when this completes correctly is:
257232709
Which I verified by scanning to notepad.
I don't know if the Operation System is relevant here, but I figured I should mention that I'm running this in Windows 7 64 and Visual Studio 2010 and .NET Framework 3.5. Scanner is a USB Barcode Scanner, Symbol LS2208, setup as "HID KEYBOARD EMULATION"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是想补充一点,microsoft POS.net,一个来自 Microsoft 的免费库,用于构建服务点系统,允许您读取最常见的条形码扫描仪,并在扫描条形码时为您提供一个事件。该库是免费的,并且得到了很好的支持,并且可以与任何具有 OPOS 驱动程序的条形码扫描仪配合使用。
just wanted to add, that microsoft POS.net, a free library from microsoft for building point of service systems, allows you to read most common barcode scanners, and gives you an event when a barcode is scanned. The library is free, and pretty well supported, and works with any barcode scanner that has an OPOS driver.
一种可以为您省去很多麻烦的非软件解决方案是为该扫描仪获取 RS-232 电缆,然后从 COM 端口读取数据。那么您就不需要关心哪个窗口或控件具有焦点。该特定扫描仪具有 COM 端口模拟驱动程序,但它可能无法在 Windows 7 下工作。
One non-software solution that could save you a lot of aggravation would be to get the RS-232 cable for this scanner and just read from the COM port. Then you wouldn't need to care which window or control had focus. That particular scanner has a COM port emulation driver, though it probably won't work under Windows 7.
我突然意识到我并不真正需要相关文章所解决的解决方案。阅读几次后,我意识到它是为了处理无法设置扫描仪以使用前缀和后缀数据的情况。对我来说幸运的是,我可以做到这一点。
否则,我与文章的作者聊天,我们认为问题是因为我运行的是 Windows 64。我认为某些代码使用了不正确的数据类型,可能会导致 64 位错误地编组数据......我无法不过要验证这一点。
不过,这对于底层 Windows 编程来说是一个很好的练习。 pinvoke.net 提供了使用互操作的丰富资源...
It occurred to me that I don't really need the solution that the article in question solves. After reading over several times i realized that it was meant to handle the situation where you couldn't setup the scanner to use prefix and suffix data. Luckily for me, I can do this.
Otherwise, I chatted with the author of the article and we believe the issue was because I was running Windows 64. I think some of the code uses incorrect datatypes that may cause 64 bit to marshal the data incorrectly... I wasn't able to verify this though.
Was a great exercise in lower level windows programming though. Great resource at pinvoke.net for using interops...