[removed] How to read a hand held barcode scanner best?

后端 未结 9 1772
温柔的废话
温柔的废话 2020-12-02 08:38

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

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 09:05

    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.

    Timer-based

    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.

    Prefix-based

    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.

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

提交回复
热议问题