I'm writing a web app for a library system with a barcode scanner attached. The scanner's input presents as keyboard input, and it's always of the form ~~[\d]+.[\d]+~~
, for example ~~470.002~~
.
I want to set up a jQuery listener for scanner input, and am a jQuery novice. It should listen to all keyboard input, but only perform an action when it hears input from the scanner, and only when the scanner input has finished.
This is as far as I've got (i.e. not very):
//Global functions: call on all pages.
$(document).ready(function() {
// Listen for scanner input.
$(window).keypress(function(e) {
var key = e.which;
if (key==126) {.
alert('tilde');
// How to listen for the correct input?
// check_out_book();
}
});
});
What's the best way to keep listening input in the format I need? I'd like it to listen for the final two tildes before calling check_out_book()
.
I'd also like it to 'stop' listening after the first tilde if there is a pause - to distinguish between a human typist and the automated scanner input. Does jQuery have a way to do this?
Any pointers very much appreciated! Thank you.