-
Notifications
You must be signed in to change notification settings - Fork 6
Home
I’m writing about this topic because almost all the articles about this on Google weren’t written well, nor were they great solutions that covered a number of barcode devices. Most were specific to certain models.
What I wanted to achieve was for a user to be be on a specific page on my website, and then by scanning a product, WITHOUT having to place focus on an input field, the webpage would recognise that the scanner had been triggered and do something with the barcode.
Many solutions talk about detecting pre-scan characters followed by the barcode then by a return code (e.g. ascii code 13). In my testing I found that the devices I had in hand did not have any pre-scan characters and while some had a return code others did not. From my reading I assume one could set pre-scan characters using something called a keyboard wedge, however I did not have the luxury of this. Just as a matter of interest to you, I tested with these USB barcode scanners: Mecer Z-3051HS and a Honeywell 3800G 14E, there were others but I do not have the model numbers on hand. I found when scanning a barcode with these devices it actually enters the characters one after each other very quickly – as if it were an extremely fast typist.
With this in mind (scanners inputting numbers very fast), it was clear the best way to generically detect a scan from one of these readers was to ensure I had a sufficient number of characters typed within a given time frame. How many numbers should be entered in quick succession to determine a scanned happened? Well for me I chose 10 or more numbers within 0.5 secs. This worked well as in practice I found some barcodes were shorter than others and generally speaking no one will really type out 10 or more numbers in under 0.5 secs.
Here is my code below. Just to recap: This code waits for input, whether it be from the keyboard or barcode scanner and tries to detect the difference between the two. It will only return a barcode scan. I will clearly comment the code below so it’s easy to understand.