How do I distinguish between a scanner input and keyboard input in Javascript?

My first answer would be to train the users not to touch the keyboard while scanning. However, the tone of your responses to answers and comments makes it sound like you're thinking more of malicious, intentional attempts to corrupt the data.

Beyond kunif's very thorough answer, you're not going to find a solution to the problem you're envisioning or running into. The reason is that JavaScript is only going to receive from the operating system's input buffer; JS will not (cannot! for OS security reasons) distinguish how the input buffer is filled. If keystrokes and scan data are simultaneously being put into the buffer, that is an issue to try to address at the OS or hardware level. JavaScript is just not equipped to deal with it.


It is not necessary to judge from keyboard/barcode scanner.
If you decide the Enter(Carriage Return) key notification as input completion on any device, you can use it as simplest trigger to execute Price Look Up/input value verification.

Most scanners can add suffix code to the scanned barcode data for notification.
The most commonly used is the Enter key, but the Tab key may also be used.
By sending the suffix code by the barcode scanner, the possibility that the scanner notification and the key input are mixed is much lower than the timeout detection.

You can do as follows.

  • Using the setting barcode, it is set to inform that keys such as Enter, Tab etc. which are not normally included in the barcode as a suffix.
  • Bind an event listener for the corresponding suffix key to the text input field.
  • The key code is judged in the event listener, and if it is the suffix key, it assumes that the input of the barcode data is complete, carries out processing such as Price Look Up/input value verification, and moves the input focus to the next field.

For example see this article.
execute function on enter key


In Addition:
Your worries seem to be overwhelmed by situations that do not occur often.

If it really happens to be a problem, you should give up dealing with JavaScript.
Please acquire scanner data with another program by the following method. Please notify it to the application in some way.

If you want to continue keyboard input emulation, it is better to capture data before the browser or application is notified.

SetWindowsHookExW function / LowLevelKeyboardProc callback function
EasyHook / Indieteur/GlobalHooks

hook into linux key event handling / uinput-mapper
The Linux keyboard driver / LKL Linux KeyLogger / kristian/system-hook
system wide keyboard hook on X under linux / Error when trying to build a Global Keyboard Hook in Ubuntu Linux / 10.5.2 Keyboard and Pointer Events


Alternatively, set the scanner to serial port mode and have a dedicated program to receive it.

Serial API
JavaScript/JQuery communicate with SerialPort/COM1

Questions tagged opos / Questions tagged pos-for-.net / Questions tagged javapos