如何检查用户输入是来自条形码扫描仪还是键盘?

Cri*_*and 8 c# io barcode-scanner winforms

我现在正在为公司自助餐厅创建pos应用程序,收银员扫描员工ID并显示其交易信息.

我的问题是收银员也可以使用他们的键盘输入(Employeeid),这是非常危险的.

if employee(true)
   show employee information
   then add orders
else
   Exception
Run Code Online (Sandbox Code Playgroud)

处理这种情况的最佳方法是什么?规则只是必须使用条形码扫描仪.

谢谢你的问候

Al-*_*mir 16

您可以监视输入代码所花费的时间.读者可以比人类输入代码更快地输入代码.

  • CTRL + C/CTRL + V打破了逻辑 (9认同)

小智 12

如果您可以修改扫描仪配置,则可以为扫描数据添加一些前缀/后缀.然后在代码中,您可以检测到这些添加的字符.

如果你做不到,那么唯一的方法是艾哈迈德 - 测量数据输入的时间.


j4x*_*j4x 11

使用RAW Input API相对容易.

看看" 在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
Run Code Online (Sandbox Code Playgroud)

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 );
Run Code Online (Sandbox Code Playgroud)

事件处理程序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.
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 它仍可在网络存档中找到:http://web.archive.org/web/20150316093927/http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard -in-的WinForms (3认同)
  • 什么是InputDevice类,你传递给构造函数的句柄是什么? (2认同)
  • https://web.archive.org/web/20130625064315/http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/此链接有效.只需要再往后走. (2认同)