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

后端 未结 9 1730
温柔的废话
温柔的废话 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条回答
  • 2020-12-02 09:07

    I'm little late but I made this work around based in some answers here.

    let code = "";
    let reading = false;
    document.addEventListener('keypress', e=>{
      //usually scanners throw an 'Enter' key at the end of read
       if (e.keyCode===13){
              if(code.length>10){
                console.log(code);
                /// code ready to use                
                code="";
             }
        }else{
             code+=e.key;//while this is not an 'enter' it stores the every key            
        }
       //run a timeout of 200ms at the first read and clear everything
        if(!reading){
             reading=true;
             setTimeout(()=>{
              code="";
              reading=false;
          }, 200);} //200 works fine for me but you can adjust it
    
    0 讨论(0)
  • 2020-12-02 09:10

    After a lot of research and testing, what worked the best for me was to capture input from a barcode scanner without focusing a form input. Listen to the keydown and textInput events.

    The textInput event acts like a paste event. It has then entire barcode data. In my case I am looking for UPC barcodes. The e.preventDefault() prevents the barcode data from being inserted into a form input:

    document.addEventListener('textInput', function (e){
        if(e.data.length >= 6){
            console.log('IR scan textInput', e.data);
            e.preventDefault();
        }
    });
    

    I have tested this on Android 4.4 and 7.0 with a CipherLab IR scanner.

    Example for listening to the keydown event. In my case I am able to assume that as long as a form input is not focused, the user is scanning a barcode.

        let UPC = '';
        document.addEventListener("keydown", function(e) {
            const textInput = e.key || String.fromCharCode(e.keyCode);
            const targetName = e.target.localName;
            let newUPC = '';
            if (textInput && textInput.length === 1 && targetName !== 'input'){
                newUPC = UPC+textInput;
    
              if (newUPC.length >= 6) {
                console.log('barcode scanned:  ', newUPC);
              } 
           }
        });
    

    Of course, rather than checking the length of the string to determine a scan, you can listen for the e.keyCode === 13 in the keydown event listener.

    Not all IR scanners will trigger the textInput event. If your device does not, then you can check to see if it is emitting something similar with:

    monitorEvents(document.body);
    

    Found this monitoring trick here: How do you log all events fired by an element in jQuery?

    0 讨论(0)
  • 2020-12-02 09:10

    I've just started working on a plugin that handles barcode scanning and credit card scanning (built on jQuery):

    https://github.com/ericuldall/jquery-pos

    Simple implementation:

    $(function(){
        $(document).pos();
        $(document).on('scan.pos.barcode', function(event){
            var barcode = event.code;
            //handle your code here....
        });
    });
    

    So far this plugin is only tested with one type of scanner and codes containing only digits, but if you have further requirements that aren't working with it, I'd be happy to adapt it to your needs. Please check out the github page and give it a whirl. Contributions are encouraged.

    E

    0 讨论(0)
提交回复
热议问题