如何检查用户输入是来自条形码扫描仪还是键盘?
我现在正在为公司自助餐厅创建pos应用程序,收银员扫描员工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.
处理此方案的最佳方法是什么? 规则只是必须使用条形码扫描仪。
谢谢你的问候
您可以监视输入代码所花费的时间。 读者可以比人类输入代码更快地输入代码。
如果您可以修改扫描仪配置,则可以为扫描数据添加一些前缀/后缀。 然后在代码中,您可以检测到这些添加的字符。
如果你做不到,那么唯一的方法是艾哈迈德 – 测量数据输入的时间。
使用RAW Input API相对容易。
看看“ 在WinForms中区分条形码扫描器和键盘 ”
我有一个程序可以读取3个不同的USB扫描仪,并将输入重定向到3个不同的“通道”进行处理。 代码有点广泛,所以我不在这里发布。 如果您愿意,我可以粘贴一些块或通过电子邮件发送给您项目。
作为线索是import:
#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. }
希望有所帮助。