优化:支持中文

This commit is contained in:
iqudoo
2026-06-09 11:05:42 +08:00
parent b12497443c
commit 13b461050b

View File

@@ -5,6 +5,28 @@ let _scannerTimer = null;
let _scannerLastInputTime = 0; let _scannerLastInputTime = 0;
const SCANNER_INPUT_INTERVAL = 100; const SCANNER_INPUT_INTERVAL = 100;
const SCANNER_IGNORE_KEYS = [
"PageUp",
"PageDown",
"ArrowRight",
"ArrowLeft",
"ArrowDown",
"ArrowUp",
"Unidentified",
"Insert",
"End",
"Home",
"Delete",
"Escape",
"Process",
"Shift",
"Backspace",
"Delete",
"Tab",
"Space",
"Control",
"CapsLock",
];
function getWindow() { function getWindow() {
if (typeof window === "undefined") { if (typeof window === "undefined") {
@@ -31,8 +53,20 @@ function delayClearScannerValue() {
}, SCANNER_INPUT_INTERVAL); }, SCANNER_INPUT_INTERVAL);
} }
function decodeAltCharCodes(value) {
return value
.replace(/Alt(\d{5})/g, (_match, code) => {
if (code.length < 5) {
return "";
}
return String.fromCharCode(Number(code));
})
.replace(/Alt(\d+)/g, () => "")
.replace(/Alt/g, () => "");
}
function normalizeScannerValue(value) { function normalizeScannerValue(value) {
return value.replace(/[\uFF01-\uFF5E]/g, char => { return decodeAltCharCodes(value).replace(/[\uFF01-\uFF5E]/g, char => {
return String.fromCharCode(char.charCodeAt(0) - 0xFEE0); return String.fromCharCode(char.charCodeAt(0) - 0xFEE0);
}).replace(/\u3002/g, ".") }).replace(/\u3002/g, ".")
.replace(/\u2014/g, "_"); .replace(/\u2014/g, "_");
@@ -64,7 +98,7 @@ function onScannerKeydown(event) {
clearScannerValue(); clearScannerValue();
return; return;
} }
if (event.ctrlKey || event.metaKey || event.altKey) { if (event.ctrlKey || event.metaKey) {
return; return;
} }
if (event.key === "Enter") { if (event.key === "Enter") {
@@ -78,7 +112,7 @@ function onScannerKeydown(event) {
} }
return; return;
} }
if (!event.key || event.key.length !== 1) { if (!event.key || SCANNER_IGNORE_KEYS.includes(event.key)) {
return; return;
} }
const now = Date.now(); const now = Date.now();
@@ -86,7 +120,8 @@ function onScannerKeydown(event) {
clearScannerValue(); clearScannerValue();
} }
_scannerLastInputTime = now; _scannerLastInputTime = now;
_scannerValue += event.key; // 累积按键序列,支持 Alt+数字 编码的中文扫码枪输入
_scannerValue = `${_scannerValue}${event.key}`;
delayClearScannerValue(); delayClearScannerValue();
} }