Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  21] [ 2]  / answers: 1 / hits: 138676  / 11 Years ago, fri, february 7, 2014, 12:00:00

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?


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.


More From » jquery

 Answers
38

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.


[#72665] Thursday, February 6, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samir

Total Points: 145
Total Questions: 90
Total Answers: 89

Location: Tokelau
Member since Sun, May 7, 2023
1 Year ago
;