Created
March 5, 2014 13:46
-
-
Save Cycymomo/9367446 to your computer and use it in GitHub Desktop.
barCodeScanner.js : detect barcode scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var pressed = false; | |
var chars = []; | |
$(window).keypress(function(e) { | |
if (e.which >= 48 && e.which <= 57) { | |
chars.push(String.fromCharCode(e.which)); | |
} | |
console.log(e.which + ":" + chars.join("|")); | |
if (pressed == false) { | |
setTimeout(function(){ | |
if (chars.length >= 10) { | |
var barcode = chars.join(""); | |
console.log("Barcode Scanned: " + barcode); | |
// assign value to some input (or do whatever you want) | |
$("#barcode").val(barcode); | |
} | |
chars = []; | |
pressed = false; | |
},500); | |
} | |
pressed = true; | |
}); | |
}); | |
$("#barcode").keypress(function(e){ | |
if ( e.which === 13 ) { | |
console.log("Prevent form submit."); | |
e.preventDefault(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment