问题描述
在一台计算机上,我有一个普通键盘和一个模拟键盘的条形码扫描仪.当我的应用获得键盘输入时,我如何确定该输入是来自条形码扫描仪还是真正的键盘?
On one computer I have both a regular keyboard and a barcode scanner which emulates a keyboard. When my app gets keyboard input, how can I determine whether that input is coming from the barcode scanner or the real keyboard?
推荐答案
你会得到双方的意见.当然,不是同时.它将全部放入队列中,但 Windows 将处理来自两个键盘的键事件.
You'll get input from both. Not simultaneously, of course. It will all be placed into a queue, but Windows will process key events from both keyboards.
不过,不要束手无策.正如 David Heffernan 所建议的那样,您可以通过将两个键盘都插入计算机,打开记事本并输入随机字符以查看哪个生成输入来轻松解决这个问题.
Don't be helpless, though. As David Heffernan suggests, you can easily figure this out yourself by plugging in both keyboards to your computer, opening up Notepad, and typing random characters to see which one generates input.
您回复说您想使用 C# 代码检查",但我不知道这意味着什么.如何创建一个从键盘读取输入并将其显示在屏幕上的控制台应用程序?
You reply that you want to "check that with C# code", but I have no idea what that means. How about creating a console app that reads input from the keyboard and displays it on the screen?
using System;
class AdvancedKeyboardTester
{
static void Main(string[] args)
{
for (; ;)
{
Console.ReadKey();
}
}
}
当您玩腻了想要退出程序时,请按 Ctrl+C.
Press Ctrl+C when you tire of the fun and want to quit the program.
听起来您正在寻找 RegisterRawInputDevices
函数,它允许您为所有键盘启用原始输入,然后枚举结果以确定哪个设备发送了消息.
It sounds like you're looking for the RegisterRawInputDevices
function, which allows you to enable raw input for all of your keyboards, and then enumerate through the results to determine which device sent the message.
幸运的是,看起来有人已经为此编写了一个 C# 包装库,可在 Code Project 上下载:使用 C# 的原始输入来处理多个键盘
Fortunately, it looks like someone has already written a C# wrapper library for this, available for download on Code Project: Using Raw Input from C# to handle multiple keyboards
编辑 2:(似乎信息只是不断从评论中欺骗)
Edit 2: (it seems the information just keeps tricking in from the comments)
如果您使用的是条形码扫描仪,这会容易得多.因为它们是专门为此目的而设计的,所以它们几乎都是可编程的.这意味着您可以告诉他们为他们的输入添加前缀(和/或后缀)一些标记字符,这些字符表明输入来自条形码扫描仪,而不是标准键盘.(查看条形码扫描仪的用户手册以获取更多信息.)然后,您所要做的就是根据这些标记字符的存在与否过滤掉键盘输入.您还可以检查输入前缀和后缀之间的字符的速度.
If you're using a barcode scanner, this gets a lot easier. Because they're explicitly designed for this purpose, they're almost all programmable. Meaning that you can tell them to prefix (and/or suffix) their input with some sentinel characters that indicate the input is coming from the barcode scanner, rather than a standard keyboard. (Check your barcode scanner's user manual for more information.) Then, all you have to do is filter out the keyboard input based on the presence or absence of those sentinel characters. You can also check for how quickly the characters between the prefix and suffix were entered.
这篇关于如何判断键盘输入是否来自条形码扫描仪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!