Skip to content

Commit afe2007

Browse files
committed
version 1.0.2, convert to latin option
1 parent 3f739ba commit afe2007

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# barcode_hid_reader
22
simple js lib for capturing events from HID barcode scanners
33

4+
demo: [https://fragsterat.github.io/barcode_hid_reader/test.html](https://fragsterat.github.io/barcode_hid_reader/test.html)
5+
46
## installing via npm:
57

68
```
@@ -25,10 +27,11 @@ function onBarcode(barcode) {
2527
// all options are optional, below is default values:
2628
let options = {
2729
timeout: 30, // timeout between symbols in ms, increase if scanning is unstable
28-
prefix: "", // barcode prefix
29-
suffix: "Enter", // barcode suffix
30-
callback: dispatchEvent, // callback. default is dispatchig custom "barcode" event
31-
log: false // boolean or callback. if true log into console, if funcion call function with arguments: state, event, event.custom
30+
prefix: "", // barcode prefix, KeyboardEvent.key see https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
31+
suffix: "Enter", // barcode suffix, KeyboardEvent.key see https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
32+
convertToLatin: true // convert barcode to latin characters
33+
callback: dispatchEvent, // callback. default is dispatching custom "barcode" event
34+
log: false // boolean or callback. if true log into console, if function call function with arguments: state, event, event.custom
3235
};
3336
3437
barcode.startCapturing(document, {

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const barcodeHidReader = (function () {
1212
let timeout
1313
let prefix
1414
let suffix
15+
let convertToLatin
1516
let callback
1617

1718
let barcode
@@ -123,7 +124,11 @@ const barcodeHidReader = (function () {
123124
)
124125

125126
if (e.key !== prefix) {
126-
barcode += e.key
127+
if (convertToLatin && e.code?.startsWith('Key')) {
128+
barcode += e.shiftKey ? e.code.charAt(3) : e.code.charAt(3).toLowerCase()
129+
} else {
130+
barcode += e.key
131+
}
127132
}
128133

129134
e.preventDefault()
@@ -138,6 +143,7 @@ const barcodeHidReader = (function () {
138143
timeout: 30,
139144
prefix: '',
140145
suffix: 'Enter',
146+
convertToLatin: true,
141147
callback: dispatchEvent,
142148
log: false
143149
}
@@ -146,7 +152,7 @@ const barcodeHidReader = (function () {
146152
defaults,
147153
startCapturing (doc, options) {
148154
logger('start capturing');
149-
({ timeout, prefix, suffix, callback, log } = Object.assign(
155+
({ timeout, prefix, suffix, callback, log, convertToLatin } = Object.assign(
150156
defaults,
151157
options
152158
))

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.1",
3+
"version": "1.0.2",
44
"description": "simple js lib for capturing data from HID barcode scanners",
55
"main": "index.js",
66
"scripts": {

test.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
let options = {
3232
prefix: form.prefix.value,
3333
suffix: form.suffix.value,
34+
convertToLatin: form.convertToLatin.checked,
3435
timeout: Number.parseInt(form.timeout.value),
3536
}
3637
console.log(options)
@@ -59,6 +60,12 @@ <h2>Настройки</h2>
5960
<input name="suffix" value="Enter" />
6061
</td>
6162
</tr>
63+
<tr>
64+
<td>Convert to latin</td>
65+
<td>
66+
<input name="convertToLatin" type="checkbox" checked />
67+
</td>
68+
</tr>
6269
<tr>
6370
<td>Timeout</td>
6471
<td>

0 commit comments

Comments
 (0)