Skip to content

Commit c0f94b3

Browse files
committed
convert to latin for non-alphabet keys
1 parent afe2007 commit c0f94b3

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ const barcodeHidReader = (function () {
2121
let node
2222
let log
2323

24+
const latinKeyCodes = {
25+
BracketLeft: ['[', '{'],
26+
BracketRight: [']', '}'],
27+
Semicolon: [';', ':'],
28+
Quote: ['\'', '"'],
29+
Backslash: ['\\', '|'],
30+
Comma: [',', '<'],
31+
Period: ['.', '>'],
32+
Slash: ['/', '?'],
33+
Digit1: ['1', '!'],
34+
Digit2: ['2', '@'],
35+
Digit3: ['3', '#'],
36+
Digit4: ['4', '$'],
37+
Digit5: ['5', '%'],
38+
Digit6: ['6', '^'],
39+
Digit7: ['7', '&'],
40+
Digit8: ['8', '*'],
41+
Digit9: ['9', '('],
42+
Digit0: ['0', ')'],
43+
Minus: ['-', '_'],
44+
Equal: ['=', '+'],
45+
Backquote: ['`', '~'],
46+
IntlBackslash: ['\\', '|']
47+
}
48+
2449
function reset () {
2550
timeoutID = undefined
2651
barcode = ''
@@ -124,8 +149,10 @@ const barcodeHidReader = (function () {
124149
)
125150

126151
if (e.key !== prefix) {
127-
if (convertToLatin && e.code?.startsWith('Key')) {
128-
barcode += e.shiftKey ? e.code.charAt(3) : e.code.charAt(3).toLowerCase()
152+
if (convertToLatin && (latinKeyCodes[e.code] || e.code?.startsWith('Key'))) {
153+
barcode += e.shiftKey
154+
? latinKeyCodes[e.code]?.[1] ?? e.code.charAt(3)
155+
: latinKeyCodes[e.code]?.[0] ?? e.code.charAt(3).toLowerCase()
129156
} else {
130157
barcode += e.key
131158
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "barcode-hid-reader",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "simple js lib for capturing data from HID barcode scanners",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)