javascript - 如何在输入标签中捕获条形码十六进制代码击键
问题描述
我有一个 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 原生应用程序。
解决方案
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.
推荐阅读
- android - 如何在 RecyclerView (Android) 中显示 imageViews
- docker - Kubernetes:容器无法 ping www.google.com
- opengl - Nvidia 上的统一寄存器要求与顶点着色器中的采样器
- mysql - 将 MySQL db 与 VB.NET 连接的 ConnectionString 是什么
- node.js - mock require of oracledb using proxyquire to unit test node.js app
- android - 如何在 Kotlin 中删除 ClipboardManager 的 OnPrimaryClipChangedListener?
- python - 如何使用 Python 将 CSV 转换为 JSON?
- java - 什么样的语句不能在服务器端准备?
- symfony4 - Symfony3.2 到 symfony4 security.encoder_factory 已弃用错误
- ios - 给定一个 IOS Bundle ID 如何显示相应的 App Icon