首页 > 解决方案 > 如何在输入标签中捕获条形码十六进制代码击键

问题描述

我有一个 Datalogic Memor10 条码扫描器,它的外观和行为很像 Android 手机,除了前面还有一个条码扫描器。条码扫描器可以通过按下 Memor10 侧面的按键来触发。它还有一个称为键盘楔的功能,幸运的是默认启用它,因为我无法弄清楚如何更改其复杂的设置。Keyboard Wedge 显然会导致任何扫描的条形码也被键入为击键;因此,例如,当您input在 Web 应用程序上有一个标签时,它会将条形码作为击键输入到该输入中。

这适用于简单的条形码,但是我要扫描的条形码是南非驾驶执照条形码,它使用 RSA 加密,因此返回的击键可以是任何字节字符。这是一个问题,因为诸如Tab和之类Enter的击键会离开输入字段或默认提交。

包含的表单标签只是为了防止 Oracle Apex 的本机表单提交。

我试图阻止这种情况但没有成功,只是在 Oracle Apex 中通过一个简单的输入屏幕将字节码作为十六进制字符。我的 Javascript/jQuery/HTML 代码目前如下:

静态内容区域:

<form method="POST" action="javascript:void(0);">
   <input type="text" id="barcode" name="barcode">
</form>

在页面加载时:

/*
$("#barcode").on('keyup', function(e) {
    console.log(e.keyCode.toString(16));
    e.preventDefault();
});
*/
$("#barcode").keypress(function(event) {
   console.log(event.which.toString(16));
   event.preventDefault();
});

然而,这并不好,因为Tab仍然离开输入字段并Enter第一次工作然后停止工作,只有在您按下另一个键时才能再次开始工作。不确定哪些其他字节码会导致问题。我可以使用什么代码成功捕获由 Memor10 条码扫描器作为击键发送到输入标签的长字节代码串?还是有其他方法可以实现这一目标?我希望这是一个 Web 应用程序,而不是 Android 原生应用程序。

标签: javascripthtmljqueryoracle-apex

解决方案


The keypress event is fired when a character value is pressed down. Also, it is deprecated. You should use the keydown event, which is fired for all keys.

<form method="POST" action="javascript:void(0);">
  <input type="text" id="barcode" name="barcode">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  
/*$("#barcode").on('keyup', function(e) {
    console.log(e.keyCode.toString(16));
    e.preventDefault();
});*/

$("#barcode").on('keydown',function(event) {
   console.log(event.which.toString(16));
   event.preventDefault();
});
</script>

This first snippet will help you prevent the tab character from causing damage.

Next, to see if there are issues with other key codes, I wrote a script which will fire an event for all key codes from 0 to 256.

$("#barcode").on('keydown',function(event) {
   console.log(event.keyCode.toString(16));
   event.preventDefault();
});

const el = document.getElementById('barcode')
for (let i = 0; i < 256; ++i) {
  el.dispatchEvent(new KeyboardEvent('keydown',{'keyCode':i}));
}
<form method="POST" action="javascript:void(0);">
  <input type="text" id="barcode" name="barcode">
</form>
<script
  src="https://code.jquery.com/jquery-3.6.0.slim.min.js"
  integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI="
  crossorigin="anonymous"></script>

It seems to work. Now you can gather all key strokes in an array to recompose your bar code.


推荐阅读