如何检查用户输入是否来自条码扫描仪或键盘?
我现在正在为公司自助餐厅创建p.o.s应用程序,其中 收银员扫描员工ID并显示其交易信息。如何检查用户输入是否来自条码扫描仪或键盘?
我的问题是收银员也可以使用他们的键盘输入(Employeeid),这是非常危险的。
if employee(true)
show employee information
then add orders
else
Exception
Currently i just hide textbox to the UI..
click New Button then cusror focus on it.
then cashier scans employeeid. <---------------in this part(The cashier can also type via keyboard) and continue transaction.
处理这种情况的最佳方法是什么?规则只有条码扫描仪必须使用。
谢谢您的问候
您可以监视输入代码的时间。读者输入代码要比输入代码快得多。
不错的理想,你可以执行更多关于代码... – kemdo 2016-07-01 08:40:53
Ctrl + c/ctrl + v打破逻辑 – 2016-08-01 18:38:23
好点,你可以监听关键事件来处理它。 – 2016-08-15 15:08:56
如果您可以修改扫描仪配置,则可以为扫描的数据添加一些前缀/后缀。然后在代码中可以检测到添加的字符。
如果你不能,那么唯一的办法就是艾哈迈德 - 测量数据输入的时间。
使用RAW Input API比较容易。
看看“Distinguishing Barcode Scanners from the Keyboard in WinForms”
我有一个程序,读取3个不同的USB扫描器和重定向输入到3个不同的“信道”进行处理。代码有点广泛,所以我不在这里发布。 如果您愿意,我可以粘贴它的一些块或通过电子邮件发送给您项目。
为线索是进口:
#region Raw Input API
[DllImport("User32.dll")]
extern static uint GetRawInputDeviceList(IntPtr pRawInputDeviceList, ref uint uiNumDevices, uint cbSize);
[DllImport("User32.dll")]
extern static uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
[DllImport("User32.dll")]
extern static bool RegisterRawInputDevices(RAWINPUTDEVICE[ ] pRawInputDevice, uint uiNumDevices, uint cbSize);
[DllImport("User32.dll")]
extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
#endregion
您添加InputDevice
到项目后,您可以通过监听事件:
// Create a new InputDevice object and register InputDevice KeyPressed event handler.
input_dev = new InputDevice(Handle);
input_dev.KeyPressed += new InputDevice.DeviceEventHandler(m_KeyPressed);
事件处理m_KeyPressed
可以让你分辨你设备通过e.Keyboard.SubClass
private void m_KeyPressed(object sender, InputDevice.KeyControlEventArgs e)
{
// e.Keyboard.SubClass tells you where from the event came.
// e.Keyboard.key gives you the input data.
}
希望能有所帮助。
什么是InputDevice类,以及传递给构造函数的是哪种类型的句柄? – 0xbadf00d 2011-03-10 17:56:08
你能提供完整的代码吗?该网站现在无法打开。 – qakmak 2016-03-07 08:42:09
它仍然可以在网络存档中找到:http://web.archive.org/web/20150316093927/http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the- keyboard-in-winforms – Alex 2016-07-28 08:53:19
什么问题?如何检测来自键盘的输入是否会帮助您?你只是要扔掉任何键盘输入?如果条码扫描器损坏或客户卡上的条形码不可读?如果他们可以用键盘手动输入数字不是更好吗?这个问题不是软件相关的;这是人类的情况。教员工不要使用键盘,除非发生紧急情况。 – 2011-02-23 08:10:06
是的你的权利..我正在想其他的方式来使应用程序更安全... – Crimsonland 2011-02-23 08:22:39
如果你想让它变得不可能,我不知道隐藏文本框有什么问题。他们无法输入隐藏文本框。如果你不想让它变得不可能,我不确定你会为安全做些什么。我无法想象这是一个真正的安全问题。如果你真的认为员工会偷吃对方的餐券,请出示图片确认。 (我不明白你对你提出的问题所作的编辑*) – 2011-02-23 08:31:53