Crimsonland
Crimsonland

Reputation: 2204

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

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

Upvotes: 11

Views: 41034

Answers (3)

j4x
j4x

Reputation: 3716

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.

Upvotes: 15

ufoq
ufoq

Reputation: 325

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.

Upvotes: 15

user105304
user105304

Reputation:

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.

Upvotes: 18

Related Questions