I\'d like to be able to scan barcodes via a hand held scanner and handle the results with Javascript.
A barcode-scanner works almost like a keyboard. It outputs the
This is an extension to the answer given by Hanz Herdel incase you are using one of the PosX scanners or any other scanner that are capable of adding a special symbol to the beginning of the characters. In this case, the tilde (~) symbol:
let barcode = "";
let reading = false;
document.addEventListener("keydown", e => {
//console.log(e.key);
if (e.key == 'Enter') {
if (barcode.length == 17) {
if (barcode.charAt(0) == '~') {
console.log(barcode);
barcode = "";
}
}
}
else {
if (e.key != 'Shift') {
barcode += e.key;
}
}
if (!reading) {
reading = true;
setTimeout( () => {
barcode = "";
reading = false;
}, 200);
}
}, true)
You can change the barcode length and the timeout speed to your liking but this worked perfect for me.
OK, so here's how I did it. I set up the scanner, to add a prefix (In my case, I used Ctrl+2 or the ascii code 002 (a control code) so it couldn't easily be entered via keyboard), and an ENTER, (Feel free to change this to use something like Ctrl+3 or ascii code 003 after each barcode scan, if your barcode data may contain enters). In jQuery, I capture the keypress event, and look for the prefix. I then, capture everything into a string, and then fire a custom event, that my app can listen for. Because I'm preventing the keypress event, the user can be in a text field, and scan a barcode, which can trigger an event without affecting anything they're doing.
Additionally, each barcode has a 1 digit prefix that we use, to identify the type of barcode scanned. Examples:
let barcodeRead = '';
let readingBarcode = false;
let handleKeyPress = (e) => {
if (e.keyCode === 2) {
// Start of barcode
readingBarcode = true;
e.preventDefault();
return;
}
if (readingBarcode) {
e.preventDefault();
if (e.keyCode === 13) { // Enter
readingBarcode = false;
const evt = $.Event('barcodeScan');
evt.state = {
type: barcodeRead.substr(0, 1),
code: barcodeRead.substr(1),
};
$(window).trigger(evt);
barcodeRead = '';
return;
}
// Append the next key to the end of the list
barcodeRead += e.key;
}
}
$(window).bind('keypress', handleKeyPress);
Because of this prefix, I can now identify the type of barcode, and see if it should be handled on this page. Example:
$(window).bind('barcodeScan', (e) => {
if (e.state.type !== 'E') {
alert('Please scan your employee badge only!');
} else {
$('#employee-badge').val(e.state.code);
}
});
A barcode-scanner works almost like a keyboard.
It depends on the model. Every one that I've used works exactly like a keyboard (at least as far as the computer is concerned)
It outputs the scanned/translated (barcode->number) data raw (right?).
It outputs keycodes.
$(document).on("scanButtonDown"
You probably want keypress
, not scanButtonDown
.
Look at the event object to determine the "key" that was pressed.
To determine when the entire code has been scanned, you might get an "end of data" key (possibly a space or a return) or you might have to just count how many characters are being input.
Here is working fine.
It's working when input has focus and input hasn't focus
on_scanner() // init function
function on_scanner() {
let is_event = false; // for check just one event declaration
let input = document.getElementById("scanner");
input.addEventListener("focus", function () {
if (!is_event) {
is_event = true;
input.addEventListener("keypress", function (e) {
setTimeout(function () {
if (e.keyCode == 13) {
scanner(input.value); // use value as you need
input.select();
}
}, 500)
})
}
});
document.addEventListener("keypress", function (e) {
if (e.target.tagName !== "INPUT") {
input.focus();
}
});
}
function scanner(value) {
if (value == '') return;
console.log(value)
}
HTML
<input type="text" id="scanner" placeholder="scanner">
var txt = "";
function selectBarcode() {
if (txt != $("#focus").val()) {
setTimeout('use_rfid()', 1000);
txt = $("#focus").val();
}
$("#focus").select();
setTimeout('selectBarcode()', 1000);
}
$(document).ready(function () {
setTimeout(selectBarcode(),1000);
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" name="tag" id="focus" placeholder="Use handheld RFID scanner">
Your pseudo code won't work, because you don't have access to the scanner to catch events like scanButtonDown
. Your only option is a HID scanner, which behaves exactly like a keyboard. To differentiate scanner input from keyboard input you have two options: Timer-based or prefix-based.
Timer-based
The scanner is likely to input characters much quicker than a user can (sensibly) with a keyboard. Calculate how quickly keystrokes are being received and buffer fast input into a variable to pass to your getProductsId
function. @Vitall wrote a reusable jQuery solution for catching barcode scanner input, you would just need to catch the onbarcodescanned event.
Prefix-based
Most scanners can be configured to prefix all scanned data. You can use the prefix to start intercepting all input and once you've got your barcode you stop intercepting input.
Full disclosure: I work as a consultant to Socket Mobile, Inc. who make handheld scanners.