Skip to content

Commit 5513291

Browse files
feat(frontend): Improve barcode scanning text input on Platform UI. (#9655)
* feat(frontend): Add control character handling to barcode text input. Ports part of 8059fb1 to Platform UI. Fixes #7529. * feat(frontend): Submit barcode scan on EOT character. Automatically submits 2D barcode scans when end of transmission character is input. --------- Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
1 parent 24a2279 commit 5513291

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/frontend/src/components/barcodes/BarcodeKeyboardInput.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { IconQrcode } from '@tabler/icons-react';
44
import { useCallback, useState } from 'react';
55
import type { BarcodeInputProps } from './BarcodeInput';
66

7+
// Control keys commonly used for ASCII control codes by barcode scanners
8+
// emulating keyboard input.
9+
// See for example: https://docs.zebra.com/us/en/scanners/general/sm72-ig/ascii-character-sets.html
10+
const BarcodeControlKeys: Record<string, string> = {
11+
KeyD: String.fromCharCode(4), // End of transmission
12+
BracketRight: String.fromCharCode(29), // Group separator
13+
Digit6: String.fromCharCode(30) // Record separator
14+
} as const;
15+
716
export default function BarcodeKeyboardInput({
817
onScan,
918
actionText = t`Scan`
@@ -32,7 +41,23 @@ export default function BarcodeKeyboardInput({
3241
setText(event.currentTarget?.value);
3342
}}
3443
onKeyDown={(event) => {
35-
if (event.code === 'Enter') {
44+
let key = event.key;
45+
46+
if (event.ctrlKey) {
47+
if (event.code in BarcodeControlKeys) {
48+
// Prevent most of the keyboard shortcuts.
49+
// Not all of them will be blocked, browser don't allow this:
50+
// https://stackoverflow.com/questions/59952382/using-preventdefault-to-override-opening-new-tab
51+
event.preventDefault();
52+
key = BarcodeControlKeys[event.code];
53+
setText((prev) => prev + key);
54+
}
55+
}
56+
57+
if (
58+
key === 'Enter' ||
59+
key === String.fromCharCode(4) // End of transmission
60+
) {
3661
onTextScan(text);
3762
}
3863
}}

0 commit comments

Comments
 (0)