从JavaScript捕获条形码读取器(键盘楔)事件

T'l*_*ash 2 javascript android barcode-scanner keyboard-wedge honeywell

我正在开发一个Web应用程序,该应用程序必须在运行Android 4.4的Honeywell Dolphin 75e设备上使用。集成的条形码读取器可以在“键盘楔形”模式下运行,但仅在文本字段具有焦点时才可以。

在桌面浏览器中,我可以使用该代码捕获条形码读取器事件:

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();
});
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不适用于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('')
    }
});
Run Code Online (Sandbox Code Playgroud)

T'l*_*ash 7

我终于收到了Honeywell支持人员的功能回复:

我怀疑应用程序希望以Keydown / Keyup事件的形式接收数据。

能请您测试以下内容吗?

在楔上作为键设置:9,10,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51, 52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76, 77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118,119,120,121,122,123,124,125

屏幕截图

由于可能需要15分钟才能手动完成,因此我创建了以下代码,您可以在“楔形作为键”字段中读取该代码:

9,10,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54, 55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,117,118,119,120,121,122,123,124,125,126,127,127

阅读代码后,请等待10秒钟再保存,然后通过退出并重新输入扫描仪设置来检查数据是否正确保存到该字段中。

最后,禁用并重新启用扫描仪(或重新启动设备)。

然后,扫描仪应可以在您的应用程序上运行。

希望这可以帮助。

终端必须使用最新版本的系统才能看到“楔入按键”字段。不要忘记将“ \ n”设置为后缀。

这样,JS代码将是:

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', 'WEDGE', BarcodesScanner.tmpTimestamp, BarcodesScanner.tmpData.length);
        BarcodesScanner.tmpTimestamp = 0;
        BarcodesScanner.tmpData = '';
    } else if (e.charCode && e.charCode > 0) {
        BarcodesScanner.tmpData += String.fromCharCode(e.charCode);
    }
});
Run Code Online (Sandbox Code Playgroud)

现在,您可以监听扫描事件:

$(BarcodesScanner).on('scan', function(e){
    alert(BarcodesScanner.barcodeData);
});
Run Code Online (Sandbox Code Playgroud)

我希望这会帮助其他人。