How to Check if User input is from Barcode Scanner or Keyboard?
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.
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.
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.