Я знаю, что могу отключить режим чтения в about:config или использовать «preventDefault» для вызова «F9», но кажется, что это так. чтобы пропустить код, который фактически предоставляет мне данные сканирования.
Вот код, который работает в Chrome (слегка измененный):
barcode-scanner .js
Код: Выделить всё
function scanListener(e) {
// add scan property to window if it does not exist
if(!window.hasOwnProperty('scan')) {
window.scan = []
}
// if key stroke appears after 10 ms, empty scan array
if(window.scan.length > 0 && (e.timeStamp - window.scan.slice(-1)[0].timeStamp) > 10) {
window.scan = []
}
//console.log(e.key);
if(e.key === "F9") {
e.preventDefault(true);
e.stopPropagation(true);
}
// if key store is enter and scan array contains keystrokes
if(e.key === "ArrowDown" && window.scan.length > 0) {
let scannedString = window.scan.reduce(function(scannedString, entry) {
return scannedString + entry.key
}, "")
// empty scan array after dispatching event
window.scan = []
// dispatch `scanComplete` with keystrokes in detail property
return document.dispatchEvent(new CustomEvent('scanComplete', {detail: scannedString}))
}
// do not listen to shift event, since key for next keystroke already contains a capital letter
// or to be specific the letter that appears when that key is pressed with shift key
if(e.key !== "Shift") {
// push `key`, `timeStamp` and calculated `timeStampDiff` to scan array
let data = JSON.parse(JSON.stringify(e, ['key', 'timeStamp']))
data.timeStampDiff = window.scan.length > 0 ? data.timeStamp - window.scan.slice(-1)[0].timeStamp : 0;
window.scan.push(data)
}
}
// document.addEventListener('scanComplete', function(e) { console.log(":)" + e.detail) })
Код: Выделить всё
document.addEventListener('keydown', scanListener);
document.addEventListener('scanComplete', function(e) {
// stuff data into object and populate form fields
});
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/791 ... code-scann
Мобильная версия