Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to read a hand held barcode scanner best?

I'd like to be able to scan barcodes via a hand held scanner and handle the results with Javascript.

A barcode-scanner works almost like a keyboard. It outputs the scanned/translated (barcode->number) data raw (right?). Actually I just need to catch the output and proceed. But how?

Here's some pseudocode I'd like to make work:

$(document).on("scanButtonDown", "document", function(e) {     // get scanned content     var scannedProductId = this.getScannedContent();      // get product      var product = getProductById(scannedProductId);      // add productname to list     $("#product_list").append("<li>" + product.name + "</li>"); }); 
  • Any ideas (frameworks, plugins, snippets)?
  • Any barcode-scanner (hardware) recommendation?

Thanks in advance!

I found this and this good questions but I'd like to get more information about the handling. Just to focus a textarea may be not enough in my case.

like image 615
Mr. B. Avatar asked Feb 07 '14 16:02

Mr. B.


People also ask

How can I improve my barcode readability?

In order to ensure barcode readability, some industries forbid highly reflective packaging and require that barcodes be printed on a white background to ensure high contrast. In certain countries or regional markets, this may actually be mandated by law or government regulations.

What is the best way to scan a barcode?

How do I scan a bar code on my phone? Go to the app store or the play store and search "barcode scan" and download any of the apps, install it and then use it. Go to your app store and search "barcode scanner" and download one. Then you can open the app and use the scanner.


1 Answers

Your pseudo code won't work, because you don't have access to the scanner to catch events like scanButtonDown. Your only option is a HID scanner, which behaves exactly like a keyboard. To differentiate scanner input from keyboard input you have two options: Timer-based or prefix-based.

Timer-based

The scanner is likely to input characters much quicker than a user can (sensibly) with a keyboard. Calculate how quickly keystrokes are being received and buffer fast input into a variable to pass to your getProductsId function. @Vitall wrote a reusable jQuery solution for catching barcode scanner input, you would just need to catch the onbarcodescanned event.

Prefix-based

Most scanners can be configured to prefix all scanned data. You can use the prefix to start intercepting all input and once you've got your barcode you stop intercepting input.

Full disclosure: I work as a consultant to Socket Mobile, Inc. who make handheld scanners.

like image 182
Enrico Avatar answered Sep 19 '22 09:09

Enrico