Skip to content

Instantly share code, notes, and snippets.

@bagaskarawg
Created December 12, 2018 08:16
Show Gist options
  • Save bagaskarawg/b75765076198a4288565291ccbaefa6d to your computer and use it in GitHub Desktop.
Save bagaskarawg/b75765076198a4288565291ccbaefa6d to your computer and use it in GitHub Desktop.
Listen for barcode scanning
var chars, pressed;
pressed = false;
chars = [];
$(window).on('keypress', function(e) {
if (e.which !== 13) {
chars.push(String.fromCharCode(e.which));
}
if (pressed === false) {
setTimeout(function() {
var barcode;
if (chars.length >= 5) {
barcode = chars.join("");
}
chars = [];
return pressed = false;
}, 300);
}
return pressed = true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment