jQuery:监听来自键盘的自动扫描仪输入?
我正在为附有条形码扫描仪的图书馆系统编写一个网络应用程序。扫描仪的输入显示为键盘输入,并且始终采用 ~~[\d]+.[\d]+~~
形式,例如 ~~470.002~~
>。
我想为扫描仪输入设置一个 jQuery 监听器,并且我是 jQuery 新手。它应该监听所有键盘输入,但仅在听到来自扫描仪的输入时并且仅在扫描仪输入完成时才执行操作。
据我所知(即不是很):
//Global functions: call on all pages.
$(document).ready(function() {
// Listen for scanner input.
$(window).keypress(function(e) {
var key = e.which;
if (key==126) {.
alert('tilde');
// How to listen for the correct input?
// check_out_book();
}
});
});
以我需要的格式继续收听输入的最佳方法是什么?我希望它在调用 check_out_book()
之前侦听最后两个波浪号。
如果有暂停,我还希望它在第一个波形符之后“停止”收听 - 以区分人类打字员和自动扫描仪输入。 jQuery 有办法做到这一点吗?
非常感谢任何指点!谢谢。
I'm writing a web app for a library system with a barcode scanner attached. The scanner's input presents as keyboard input, and it's always of the form ~~[\d]+.[\d]+~~
, for example ~~470.002~~
.
I want to set up a jQuery listener for scanner input, and am a jQuery novice. It should listen to all keyboard input, but only perform an action when it hears input from the scanner, and only when the scanner input has finished.
This is as far as I've got (i.e. not very):
//Global functions: call on all pages.
$(document).ready(function() {
// Listen for scanner input.
$(window).keypress(function(e) {
var key = e.which;
if (key==126) {.
alert('tilde');
// How to listen for the correct input?
// check_out_book();
}
});
});
What's the best way to keep listening input in the format I need? I'd like it to listen for the final two tildes before calling check_out_book()
.
I'd also like it to 'stop' listening after the first tilde if there is a pause - to distinguish between a human typist and the automated scanner input. Does jQuery have a way to do this?
Any pointers very much appreciated! Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您希望通过存储到目前为止收到的内容并检查其是否有效来做到这一点。如果不是,请丢弃您收到的内容并重新开始:
您可以将所有这些正则表达式合并为两个,但我认为这种方式更清晰。
I think you want to do this by storing what you've received so far and checking to see if it's valid. If it is not, discard what you have received and start again:
You could combine all those regexps into two, but I think it's more legible this way.
我刚刚写完一段 JavaScript,用于检测是否使用条形码扫描仪来填写输入字段,如果是,则将焦点移动到下一个字段。
我的代码回答了您的部分问题,“我想为扫描仪输入设置一个 jQuery 侦听器,并且我是 jQuery 新手。它应该侦听所有键盘输入,但仅在听到来自扫描仪的输入时才执行操作,并且仅在当扫描仪输入完成时。”
以下是输入字段的 HTML:
我有两个带有“bcode”类的字段,用于条形码扫描仪输入(f1 和 f2)。第三个字段用于常规输入 (f3)。字段 f1 和f2 将 (1) 按下某个键时的当前时间戳发送给函数“typeSpeed”,以及 (2) 当字段获得焦点时要选择的下一个字段的 id。当字段失去焦点时,这些字段还会触发对函数“typeSpeedReset”的调用。
下面是使其工作的 javascript/jQuery:
发生的情况是按键之间的时间由函数“typeSpeed”进行量化。我通过实验(敲击键盘或按住某个键)发现,最快的人类输入在击键之间有大约 33 毫秒的延迟。我用来测试的条码扫描仪通常会产生 10 毫秒或更短的延迟。
在具有“bcode”类的字段上设置超时,以检测输入何时暂时停止。此时,评估延迟。如果小于 20 毫秒,则假定已使用条形码扫描仪,并给予下一个字段焦点。
编写此代码的项目更进一步,更改字段的背景颜色,并在获得焦点时在字段右侧显示一个小的条形码图形,以便用户清楚地表明它响应并适用于条形码扫描仪输入。
I just finished writing a bit of javascript that detects whether a barcode scanner was used to fill in an input field and moves focus to the next field if it was.
My code answers part of your question, "I want to set up a jQuery listener for scanner input, and am a jQuery novice. It should listen to all keyboard input, but only perform an action when it hears input from the scanner, and only when the scanner input has finished."
Here is the HTML for the input fields:
I have two fields with class 'bcode' that are intended for barcode scanner entry (f1 & f2). The third field is intended for regular input (f3). Fields f1 & f2 send (1) the current timestamp when a key is pressed to the function 'typeSpeed' and (2) the id of the next field to select when the field gains focus. These fields also trigger a call to function 'typeSpeedReset' when the field loses focus.
Here is the javascript/jQuery that makes it work:
What happens is the time between keystrokes is quantified by the function 'typeSpeed'. I found out through experimentation (mashing the keyboard or holding down a key) that the fastest human input has approximately ~33ms of delay between keystrokes. The barcode scanner I used to test with generally produced delays of 10ms or less.
A timeout is set on the field with class 'bcode' to detect when input has stopped momentarily. At this point, the delay is evaluated. If it is less than 20ms, a barcode scanner is assumed to have been used and the next field is given focus.
The project this code was written for goes a step further by changing the background color of the field and displaying a small barcode graphic to the right of the field when it has focus so users have a clear indication that it's responsive to and intended for barcode scanner input.