11

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

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Crimsonland
  • 2,194
  • 3
  • 24
  • 42
  • 3
    What's the question? How is detecting that the input is from the keyboard going to help you? Are you just going to throw away any keyboard input? What if the barcode scanner is broken, or the barcode on the customer's card is not readable? Wouldn't it be better if they could enter the number manually with the keyboard? This problem isn't software-related; it's a human situation. Teach the employees not to use the keyboard, except in case of emergency. – Cody Gray - on strike Feb 23 '11 at 08:10
  • Yup your right..i am thinking other ways to make the app more secure... – Crimsonland Feb 23 '11 at 08:22
  • If you're trying to make it impossible, I'm not sure what's wrong with hiding the textbox. They can't type into a hidden textbox. If you don't want to make it impossible, I'm not sure what else you'd do for security. I can't imagine this is a real security concern. Pop up a picture verification if you really think employees are going to be stealing each other's meal tickets. (I don't understand the edits you made to your question *at all*.) – Cody Gray - on strike Feb 23 '11 at 08:31
  • I already did Employee Information verification..Ahmed Suggestion is right...Thanks – Crimsonland Feb 27 '11 at 23:20
  • 1
    A common scenario is that a barcode won't scan, and the employee must manually key in the ID. I see that a lot at the grocery checkout. Scanners break, too. Finally, this means you'll need a working scanner for UI testing. – TrueWill Feb 27 '11 at 23:45
  • https://stackoverflow.com/questions/10850050/get-barcode-reader-value-form-background-monitoring – hamid reza mansouri Apr 05 '18 at 13:21
  • If you are the one who is generating the barcode of EmployeeId you can start with the special character like * % etc. on start of the code(Barcode), then just detect/identify either the Text Box first value is starting from the special character or not, in this way you can simply differentiate the both Input values. Regards... S. Nasir – Faheem Nasir Apr 29 '18 at 04:29

3 Answers3

18

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.

15

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.

ufoq
  • 325
  • 2
  • 12
15

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.

Butzke
  • 561
  • 1
  • 14
  • 30
j4x
  • 3,595
  • 3
  • 33
  • 64
  • 2
    What's the InputDevice class and what kind of handle are you passing to the constructor? – 0xbadf00d Mar 10 '11 at 17:56
  • Could you provide the full code please? the website can't open now. – qakmak Mar 07 '16 at 08:42
  • 3
    It is still available in the web archive: http://web.archive.org/web/20150316093927/http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms – ralien Jul 28 '16 at 08:53
  • I know it was long time ago, but maybe do you still have this project alive? – makaroN Feb 02 '17 at 13:17
  • Hello @Sybren. Unfortunatelly I don't own the full code that I implemented neither I work for the same company anymore. Unfortunatelly the original reference is gone and I couldn't find any other working source of it. Well, all you need to recognise the input source is posted in my answer and implement a program using it is quite straightforward. What exactly do you need to make your program work? – j4x Sep 08 '17 at 16:23
  • Hi @j4x. Thanks for your reply. I understand I need the mentioned imports. For me `InputDevice` and `Handle` are unclear. Where do they come from? My goal is to recognize an usb barcode scanner acting as a keyboard wedge (and capture the keystrokes). Is it possible with your code example? – Sybren Sep 08 '17 at 16:31
  • @Sybren. InputDevice: https://msdn.microsoft.com/en-us/library/system.windows.input.inputdevice(v=vs.110).aspx. Handle is your Form.Handle. – j4x Sep 08 '17 at 16:56
  • Okay thanks, I will try it next week when I'm back at work. My app is a wpf app but I guess it works the same. This will work for an usb keyboard wedge barcode scanner? – Sybren Sep 08 '17 at 22:22
  • @j4x `InputDevice` is an abstract class? `IntPtr handle = new WindowInteropHelper(Application.Current.MainWindow).Handle; InputDevice id = new InputDevice(handle);` this doesn't work – Sybren Sep 09 '17 at 08:07
  • 2
    https://web.archive.org/web/20130625064315/http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/ this link works. Just need to go further back. – Blackclaws Oct 18 '17 at 06:29