Javascript:如何最好地读取手持条码扫描器? [英] Javascript: How to read a hand held barcode scanner best?

查看:45
本文介绍了Javascript:如何最好地读取手持条码扫描器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过手持扫描器扫描条形码并使用 Javascript 处理结果.

I'd like to be able to scan barcodes via a hand held scanner and handle the results with Javascript.

条码扫描器的工作原理几乎就像键盘一样.它输出扫描/翻译(条形码->数字)原始数据(对吗?).实际上我只需要捕捉输出并继续.但是如何?

A barcode-scanner works almost like a keyboard. It outputs the scanned/translated (barcode->number) data raw (right?). Actually I just need to catch the output and proceed. But how?

这是一些我想使用的伪代码:

Here's some pseudocode I'd like to make work:

$(document).on("scanButtonDown", "document", function(e) {
    // get scanned content
    var scannedProductId = this.getScannedContent();

    // get product 
    var product = getProductById(scannedProductId);

    // add productname to list
    $("#product_list").append("<li>" + product.name + "</li>");
});

  • 任何想法(框架、插件、片段)?
  • 任何条码扫描器(硬件)推荐?
  • 提前致谢!

    我发现了这个this 很好的问题,但我想获得有关处理的更多信息.在我的情况下,仅仅关注 textarea 可能还不够.

    I found this and this good questions but I'd like to get more information about the handling. Just to focus a textarea may be not enough in my case.

    推荐答案

    您的伪代码将无法工作,因为您无权使用扫描器来捕获诸如 scanButtonDown 之类的事件.您唯一的选择是 HID 扫描仪,它的行为与键盘完全相同.要区分扫描仪输入和键盘输入,您有两种选择:基于定时器或基于前缀.

    Your pseudo code won't work, because you don't have access to the scanner to catch events like scanButtonDown. Your only option is a HID scanner, which behaves exactly like a keyboard. To differentiate scanner input from keyboard input you have two options: Timer-based or prefix-based.

    基于定时器

    扫描仪输入字符的速度可能比用户(明智地)使用键盘输入的速度快得多.计算接收击键的速度并将快速输入缓冲到变量中以传递给您的 getProductsId 函数.@Vitall 编写了一个用于捕获条码扫描仪输入的可重用 jQuery 解决方案,您只需要捕获 onbarcodescanned 事件.

    The scanner is likely to input characters much quicker than a user can (sensibly) with a keyboard. Calculate how quickly keystrokes are being received and buffer fast input into a variable to pass to your getProductsId function. @Vitall wrote a reusable jQuery solution for catching barcode scanner input, you would just need to catch the onbarcodescanned event.

    基于前缀

    大多数扫描仪都可以配置为所有扫描数据的前缀.您可以使用前缀开始拦截所有输入,一旦获得条形码,您就停止拦截输入.

    Most scanners can be configured to prefix all scanned data. You can use the prefix to start intercepting all input and once you've got your barcode you stop intercepting input.

    完全披露:我是 Socket Mobile, Inc. 的顾问,该公司制造手持式扫描仪.

    Full disclosure: I work as a consultant to Socket Mobile, Inc. who make handheld scanners.

    这篇关于Javascript:如何最好地读取手持条码扫描器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆