I'm trying to create a keypress/keyup function which reads from the document (not from an input field) - I'm gonna use it for a barcode scanning device.
$(document).keyup(function(e) {
var barcode = e.which;
// the barcode should be without the enter at the end
if (e.keyCode == 13){
// stop reading
}
});
I'm quite new to javascript and jQuery - as you probably already noticed.
Thanks for every help!
This worked for me:
$(function(){
var barcode = '';
var getCode = function (e) {
if (e.which != 13) {
barcode += String.fromCharCode(e.which);
} else {
$(document).unbind('keyup', getCode);
console.log(barcode);
}
};
$(document).on('keypress', getCode);
// Barcode ausgeben
// Produktinfos ausgeben
$('span#articleID').text('articleID');
$('span#productName').text('productName');
});
Thanks all!!
Create a function to bind on keyup. When enter is pressed, just unbind the function to make sure the code won't change anymore.
var barcode = '';
var getCode = function (e) {
if (e.which != 13) {
barcode += e.which;
} else {
$(document).unbind('keyup', getCode);
console.log(barcode);
}
};
$(document).on('keyup', getCode);