I need to handle usb barcode scanner in my web page, all was working well on my computer but when i tried it on another one (Windows 7 and another scanner) i had an issue with shift key.
The scanner barcode has : symbol and when it's read it's passed like Shift; how can i get the real symbol from it?
Here is my function when i handle the scanning:
document.addEventListener("keyup", (event) => {
let key = event.key;
if (event.isComposing || key === 'Shift' || key === 'Control' || key === 'Meta' || key === 'Alt') {
return;
}
if (containerLoading.classList.contains("d-none")) {
showLoading();
}
if (!containerLoading.classList.contains("d-none")) {
if (key === "Enter" || key === "Tab") {
if (barcode != "") {
websocket.send(`<ART>${barcode}</ART>`);
barcode = "";
}
} else {
barcode += key;
}
}
});
Here is how the scanner barcode result in notepad (and that's how it should be) and under you can find how it looks like in the browser:
TEST TEXT:
WRONG
Hc1ò6BFOXN%Ts3DHPVo13j -g'-2YRVa.q-r8'd32Fc1j9M$Di9CPi9ELNLAPS$SçLc-GPWBILc9Ff9
84Ev28JVSAp921TDUMx6WUKAMa800p92ò3
RIGHT
HC1ç6BFOXN%TS3DHPVO13J -G'-2YRVA.Q-R8'D32FC1J9M$DI9CPI9ELNLAPS$SçLC-GPWBILC9FF9
(4EV28JVSAP921TDUMX6WUKAMA800P92ç3
ENG KEYBOARD
HC1:6BFOXN%TS3DHPVO13J /G-/2YRVA.Q/R8-D32FC1J9M$DI9CPI9ELNLAPS$S:LC/GPWBILC9FF9
*4EV28JVSAP921TDUMX6WUKAMA800P92:3
Solved by replacing keyup with keypress
Now the code looks like this:
document.addEventListener("keypress", (event) => {
let key = event.key;
if (event.isComposing || key === 'Shift' || key === 'Control' || key === 'Meta' || key === 'Alt') {
return;
}
if (containerLoading.classList.contains("d-none")) {
showLoading();
}
if (!containerLoading.classList.contains("d-none")) {
if (key === "Enter" || key === "Tab") {
if (barcode != "") {
websocket.send(`<ART>${barcode}</ART>`);
barcode = "";
}
} else {
barcode += key;
}
}
});