How to Check if User input is from Barcode Scanner or Keyboard?

寵の児 提交于 2019-12-17 17:58:17

问题


I am creating a p.o.s application for a cafeteria company in which the cashier scans his employee ID and it shows his information for the transaction.

My Problem is, the cashier can also use their keyboard for their input (employee ID) which is very risky.

if employee(true)
   show employee information
   then add orders
else
   Exception

Currently I just hide TexTbox from the UI, click New Button then set cursor focus on it. Then cashier scans employee id. In this part, the cashier can also type via keyboard and continue transaction.

What is the best way to handle this scenario? The rule is only barcode scanner must be use.

Thanks in regards


回答1:


You could monitor the time it took for the code to be entered. A reader would enter the code much faster than a human typing it in.




回答2:


If you have the possibility to modify the scanner configuration you can add some prefix/suffix to the scanned data. Then in the code you can detect those added characters.

If you can't, then only way is Ahmed's - measuring the time of data entry.




回答3:


It is relatively easy done with RAW Input API.

Take a look at "Distinguishing Barcode Scanners from the Keyboard in WinForms"

I have a program that reads 3 different USB scanners and redirects the input to 3 different "channels" for processing. The code is somewhat extensive, so I am not postin it here. If you wish, I can paste some chunks of it or send you the project in e-mail.

As a clue are the imports:

#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

After you add the InputDevice to your project, you can listen to events by:

// Create a new InputDevice object and register InputDevice KeyPressed event handler.
input_dev = new InputDevice( Handle );
input_dev.KeyPressed += new InputDevice.DeviceEventHandler( m_KeyPressed );

The event handler m_KeyPressed lets you to distinguish your devices through 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.
}

Hope to have helped.



来源:https://stackoverflow.com/questions/5088374/how-to-check-if-user-input-is-from-barcode-scanner-or-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!