Detect when input box filled by keyboard and when by barcode scanner.

前端 未结 11 2050
星月不相逢
星月不相逢 2020-12-01 00:44

How I can programmatically detect when text input filled by typing on keyboard and when it filled automatically by bar-code scanner?

相关标签:
11条回答
  • 2020-12-01 01:31

    If you can set a prefix to your barcode scanner I suggests this (I changed a bit the Vitall code):

    var BarcodeScanner = function(options) {
         this.initialize.call(this, options);
    };
    BarcodeScanner.prototype = {
        initialize: function(options) {
           $.extend(this._options,options);
           if(this._options.debug) console.log("BarcodeScanner: Initializing");
           $(this._options.eventObj).on({
              keydown: $.proxy(this._keydown, this),
           });
        },
        destroy: function() {
            $(this._options.eventObj).off("keyup",null,this._keyup);
            $(this._options.eventObj).off("keydown",null,this._keydown);
        },
        fire: function(str){
            if(this._options.debug) console.log("BarcodeScanner: Firing barcode event with string: "+str);
            $(this._options.fireObj).trigger('barcode',[str]);
        },
        isReading: function(){
            return this._isReading;
        },
        checkEvent: function(e){
            return this._isReading || (this._options.isShiftPrefix?e.shiftKey:!e.shiftKey) && e.which==this._options.prefixCode;
        },
        _options: {timeout: 600, prefixCode: 36, suffixCode: 13, minCode: 32, maxCode: 126, isShiftPrefix: false, debug: false, eventObj: document, fireObj: document},
        _isReading: false,
        _timeoutHandler: false,
        _inputString: '',
        _keydown: function (e) {
            if(this._input.call(this,e))
                return false;
        },
        _input: function (e) {
            if(this._isReading){
                if(e.which==this._options.suffixCode){
                    //read end
                    if(this._options.debug) console.log("BarcodeScanner: Read END");
                    if (this._timeoutHandler) 
                        clearTimeout(this._timeoutHandler);
                    this._isReading=false;
                    this.fire.call(this,this._inputString);
                    this._inputString='';
                }else{
                    //char reading
                    if(this._options.debug) console.log("BarcodeScanner: Char reading "+(e.which));
                    if(e.which>=this._options.minCode && e.which<=this._options.maxCode)
                        this._inputString += String.fromCharCode(e.which);
                }
                return true;
            }else{
                if((this._options.isShiftPrefix?e.shiftKey:!e.shiftKey) && e.which==this._options.prefixCode){
                    //start reading
                    if(this._options.debug) console.log("BarcodeScanner: Start reading");
                    this._isReading=true;
                    this._timeoutHandler = setTimeout($.proxy(function () {
                        //read timeout
                        if(this._options.debug) console.log("BarcodeScanner: Read timeout");
                        this._inputString='';
                        this._isReading=false;
                        this._timeoutHandler=false;
                    }, this), this._options.timeout);
                    return true;
                }
            }
            return false;
        }
    };
    

    If you need you customize timeout, suffix, prefix, min/max ascii code readed:

    new BarcodeScanner({timeout: 600, prefixKeyCode: 36, suffixKeyCode: 13, minKeyCode: 32, maxKeyCode: 126});
    

    I also added the isShiftPrefix option to use for example the $ char as prefix with these options: new BarcodeScanner({prefixKeyCode: 52, isShiftPrefix: true});

    This is a fiddle: https://jsfiddle.net/xmt76ca5/

    0 讨论(0)
  • 2020-12-01 01:37

    $(window).ready(function(){
    
    	//$("#bCode").scannerDetection();
    
    	console.log('all is well');
    	
    	$(window).scannerDetection();
    	$(window).bind('scannerDetectionComplete',function(e,data){
                console.log('complete '+data.string);
                $("#bCode").val(data.string);
            })
            .bind('scannerDetectionError',function(e,data){
                console.log('detection error '+data.string);
            })
            .bind('scannerDetectionReceive',function(e,data){
                console.log('Recieve');
                console.log(data.evt.which);
            })
    
            //$(window).scannerDetection('success');
    <input id='bCode'type='text' value='barcode appears here'/>

    0 讨论(0)
  • 2020-12-01 01:42

    The solution from Vitall only works fine if you already hit at least one key. If you don't the first character will be ignored (if(this._timeoutHandler) returns false and the char will not be appended).

    If you want to begin scanning immediately you can use the following code:

    var BarcodeScanerEvents = function() {
    	this.initialize.apply(this, arguments);
    };
    
    BarcodeScanerEvents.prototype = {
    	initialize : function() {
    		$(document).on({
    			keyup : $.proxy(this._keyup, this)
    		});
    	},
    	_timeoutHandler : 0,
    	_inputString : '',
    	_keyup : function(e) {
    		if (this._timeoutHandler) {
    			clearTimeout(this._timeoutHandler);
    		}
    		this._inputString += String.fromCharCode(e.which);
    
    		this._timeoutHandler = setTimeout($.proxy(function() {
    			if (this._inputString.length <= 3) {
    				this._inputString = '';
    				return;
    			}
    
    			$(document).trigger('onbarcodescaned', this._inputString);
    
    			this._inputString = '';
    
    		}, this), 20);
    	}
    };

    0 讨论(0)
  • 2020-12-01 01:42

    You can use a "onkeyup" event on that input box. If the event has triggered then you can called it "Input from Keyboard".

    0 讨论(0)
  • 2020-12-01 01:44

    you can try following example, using jQuery plugin https://plugins.jquery.com/scannerdetection/

    Its highly configurable, time based scanner detector. It can be used as solution for prefix/postfix based, time based barcode scanner.

    Tutorial for usage and best practices, as well discussed about various Barcode Scanner Models and how to deal with it. http://a.kabachnik.info/jquery-scannerdetection-tutorial.html

    $(window).ready(function(){
    
    	//$("#bCode").scannerDetection();
    
    	console.log('all is well');
    	
    	$(window).scannerDetection();
    	$(window).bind('scannerDetectionComplete',function(e,data){
                console.log('complete '+data.string);
                $("#bCode").val(data.string);
            })
            .bind('scannerDetectionError',function(e,data){
                console.log('detection error '+data.string);
            })
            .bind('scannerDetectionReceive',function(e,data){
                console.log('Recieve');
                console.log(data.evt.which);
            })
    
            //$(window).scannerDetection('success');
    <input id='bCode'type='text' value='barcode appears here'/>

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