I just finished writing a bit of javascript that determines if a barcode scanner was used to populate the input field and moves focus to the next field if it was.
My code answers part of your question: “I want to configure the jQuery listener to enter the scanner and start starting with jQuery. It should listen to all the keyboard input, but only perform the action when it hears the input from the scanner, and only after the scanner is finished entering. "
Here is the HTML for the input fields:
<input type="text" class="bcode" id="f1" onkeydown="typeSpeed(new Date().getTime());" onblur="typeSpeedReset();" onfocus="typeNextField('f2');" /> <input type="text" class="bcode" id="f2" onkeydown="typeSpeed(new Date().getTime());" onblur="typeSpeedReset();" onfocus="typeNextField('f3');" /> <input type="text" id="f3" />
I have two fields with the "bcode" class, designed to write a barcode scanner (f1 and f2). The third field is for regular input (f3). Fields f1 and f2 send (1) the current timestamp when a key is pressed on the typeSpeed function and (2) the identifier of the next field to select when the field receives focus. These fields also call the "typeSpeedReset" function when the field loses focus.
Here is the javascript / jQuery that makes it work:
$(function(){ var typingTimeout; $('.bcode').keypress(function(e){ if (typingTimeout != undefined) clearTimeout(typingTimeout); typingTimeout = setTimeout(isBarcode, 500); }); }); var ts0 = 0, ts1 = 0, ts2, nf; function typeSpeed(time) { ts0 = (ts0 == 0) ? time : 0; var ts3 = ts1; ts1 = time - ts0; ts2 = ts1 - ts3; } function typeSpeedReset() { ts0 = 0; ts1 = 0; } function typeNextField(nextfield) { nf = nextfield; } function isBarcode() { if(ts2 < 20 && ts1 != 0) { $('#'+nf).focus(); } }
What happens is the time between keystrokes is quantified by the typeSpeed function. I found that the experiment (crushing the keyboard or pressing a key) that the fastest human input has approximately ~ 33 ms delay between keystrokes. The barcode scanner I used for testing with typically produced delays of 10 ms or less.
A timeout is set in the field with the "bcode" class to detect when the input is stopped momentarily. At this point, a delay estimate is performed. If it is less than 20 ms, it is assumed that a barcode scanner is being used, and the next field is focused.
A project written for this code goes even further by changing the background color of the field and displaying a small barcode to the right of the field when it has focus, so users have a clear indication that it is responding to the barcode scanner.