我正在開發一個必須在運行Android 4.4的Honeywell Dolphin 75e設備上使用的Web應用程序。 集成的條形碼閱讀器可以在「鍵盤楔形」模式下操作,但只能在文本字段有焦點時使用。從javascript中捕獲條形碼讀取器(鍵盤 - 楔形)事件
與桌面瀏覽器,我可以使用代碼來捕捉條形碼閱讀器事件:
var BarcodesScanner = {
barcodeData: '',
deviceId: '',
symbology: '',
timestamp: 0,
dataLength: 0
};
function onScannerNavigate(barcodeData, deviceId, symbology, timestamp, dataLength){
BarcodesScanner.barcodeData = barcodeData;
BarcodesScanner.deviceId = deviceId;
BarcodesScanner.symbology = symbology;
BarcodesScanner.timestamp = timestamp;
BarcodesScanner.dataLength = dataLength;
$(BarcodesScanner).trigger('scan');
}
BarcodesScanner.tmpTimestamp = 0;
BarcodesScanner.tmpData = '';
$(document).on('keypress', function(e){
e.stopPropagation();
var keycode = (e.keyCode ? e.keyCode : e.which);
if (BarcodesScanner.tmpTimestamp < Date.now() - 500){
BarcodesScanner.tmpData = '';
BarcodesScanner.tmpTimestamp = Date.now();
}
if (keycode == 13 && BarcodesScanner.tmpData.length > 0){
onScannerNavigate(BarcodesScanner.tmpData, 'FAKE_SCANNER', '', BarcodesScanner.tmpTimestamp, BarcodesScanner.tmpData.length);
BarcodesScanner.tmpTimestamp = 0;
BarcodesScanner.tmpData = '';
} else if (e.charCode && e.charCode > 0) {
BarcodesScanner.tmpData += String.fromCharCode(e.charCode);
}
});
$(BarcodesScanner).on('scan', function(e){
alert();
});
不幸的是,它並沒有在Android上工作。 是否有API允許我捕獲這些事件? 或處理這個問題的其他瀏覽器?
編輯:
我能夠攔截使用文本字段作爲緩衝條形碼閱讀器的事件。
但在這種情況下,我不能使用任何需要我的應用程序中的焦點的控件。這是一個障礙。
BarcodesScanner.tmpInput = $('<input />', {
type: 'text',
style: 'position: fixed; top: 0; right: 0; width: 0; height: 0;'
});
$('body').append(BarcodesScanner.tmpInput);
setInterval(function(){
BarcodesScanner.tmpInput.focus();
}, 500);
BarcodesScanner.tmpInput.on('input', function(e){
if (BarcodesScanner.tmpInput.val().length > 0){
onScannerNavigate(BarcodesScanner.tmpInput.val(), 'FAKE_SCANNER', 'WEDGE', Date.now(), BarcodesScanner.tmpInput.val().length);
BarcodesScanner.tmpInput.val('')
}
});
謝謝你的分享,這是很棒的東西,我正在尋找同樣的東西。 Hower,在我的應用程序中,每次掃描時都會顯示鍵盤。這也發生在你身上嗎?有關如何擺脫它的任何建議? – user1051218
通常,鍵盤僅在輸入字段中顯示,那麼您是使用本答案中公開的捕獲解決方案還是原始問題編輯中的文本字段緩衝區?您可以使用空鍵盤(https://play.google.com/store/apps/details?id=com.wparam.nullkeyboard&hl=fr)等應用停用它。 什麼是您的設備? –
對於沒有「楔子作爲鑰匙」選項的Dolphin 75e用戶,您需要聯繫霍尼韋爾支持以獲取最新的固件,因爲由於授權問題,它們不在其網站上提供。相關文件是'PARISAD_56.01.13.0173.zip'。 – SystemParadox