2012-07-02 8 views
11

कुंजीपटल पर टाइप करके भरने वाले टेक्स्ट इनपुट और बार-कोड स्कैनर द्वारा स्वचालित रूप से भरने पर मैं प्रोग्रामेटिक रूप से कैसे पता लगा सकता हूं?पता लगाएं कि कीबोर्ड द्वारा भरने वाले इनपुट बॉक्स और बारकोड स्कैनर द्वारा कब।

उत्तर

4

खैर एक बारकोड किसी भी महत्वपूर्ण घटनाओं सक्रिय नहीं होगा तो आप की तरह कुछ कर सकता है: जब आप इस का मूल्यांकन करना चाहते पर निर्भर करता है

$('#my_field').on({ 
    keypress: function() { typed_into = true; }, 
    change: function() { 
     if (typed_into) { 
      alert('type'); 
      typed_into = false; //reset type listener 
     } else { 
      alert('not type'); 
     } 
    } 
}); 

, आप परिवर्तन पर नहीं बल्कि पर प्रस्तुत इस चेक करने के लिए चाहते हो सकता है , जो कुछ भी।

+0

धन्यवाद। लेकिन जब बारकोड इनपुट था तो मैं कैसे पता लगा सकता हूं? 'ऑनेंज' घटना तब बढ़ती है जब तत्व फोकस खो देता है, लेकिन मुझे डालने के तुरंत बाद डालने वाले मूल्य को जानने की आवश्यकता होती है। मैं टाइमर बनाने और समय-समय पर टेक्स्ट फ़ील्ड पर मूल्य की जांच कर सकता हूं, लेकिन मुझे लगता है कि यह गलत समाधान है। – MyTitle

+0

जब आपने इनपुट जांचना चाहते थे तो आपने अपने प्रश्न में उल्लेख नहीं किया था, इसलिए मैंने चेंज पर विचार किया। हां, मुझे लगता है कि एक टाइमर शायद एकमात्र तरीका है, जब तक कि बारकोड एप्लिकेशन किसी प्रकार की कस्टम घटना को तब तक चलाता है जब यह पृष्ठ में हेरफेर करता है। – Utkanos

1

आप उस इनपुट बॉक्स पर "ऑनकीप" ईवेंट का उपयोग कर सकते हैं। यदि ईवेंट ट्रिगर हुआ है तो आप इसे "कीबोर्ड से इनपुट" कह सकते हैं।

18

मैंने यह जवाब लिखा, क्योंकि मेरा बारकोड स्कैनर मोटोरोला एलएस 12020 जेनरेट की गई कुंजीपटल घटना है, इसलिए मैं यूटानोस के समाधान का उपयोग नहीं कर सकता।

मेरे समाधान है:

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); 
    } 
}; 
1

मैं पता चलता है कि आप अपने बारकोड स्कैनर के लिए एक उपसर्ग सेट कर सकते हैं यदि यह (मैं थोड़ा Vitall कोड बदल):

var BarcodeScanner = function(options) { 
    this.initialize.call(this, options); 
}; 

BarcodeScanner.prototype = { 
    initialize: function(options) { 
     $.extend(this._options,options); 
     $(document).on({ 
      keyup: $.proxy(this._keyup, this) 
     }); 
    }, 
    fire: function(str){ 
     $(document).trigger('barcode',str); 
    }, 
    _options: {timeout: 600, prefixKeyCode: 36, suffixKeyCode: 13, minKeyCode: 32, maxKeyCode: 126}, 
    _isReading: false, 
    _timeoutHandler: false, 
    _inputString: '', 
    _keyup: function (e) { 
     if(this._isReading){ 
      if(e.keyCode==this._options.suffixKeyCode){ 
       //read end 
       if (this._timeoutHandler) 
        clearTimeout(this._timeoutHandler); 
       this._isReading=false; 
       this.fire.call(this,this._inputString); 
       this._inputString=''; 
      }else{ 
       //char reading 
       if(e.which>=this._options.minKeyCode && e.which<=this._options.maxKeyCode) 
        this._inputString += String.fromCharCode(e.which); 
      } 
     }else{ 
      if(e.keyCode==this._options.prefixKeyCode){ 
       //start reading 
       this._isReading=true; 
       this._timeoutHandler = setTimeout($.proxy(function() { 
        //read timeout 
        this._inputString=''; 
        this._isReading=false; 
        this._timeoutHandler=false; 
       }, this), this._options.timeout); 
      } 
     } 
    } 
}; 

मानक उपसर्ग के साथ उपयोग (होम कुंजी) और प्रत्यय (कुंजी ENTER):

new BarcodeScanner(); 
... 
$(document).on('barcode',function(e,str){ 
    console.log(str);  
}); 

क्या आप समय समाप्त, प्रत्यय, उपसर्ग, न्यूनतम/अधिकतम ascii अनुकूलित की जरूरत है कोड readed:

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

आप उदाहरण निम्नलिखित कोशिश कर सकते हैं, jQuery प्लगइन का उपयोग कर https://plugins.jquery.com/scannerdetection/

इसकी अत्यधिक विन्यास, समय आधारित स्कैनर डिटेक्टर। इसे उपसर्ग/पोस्टफिक्स आधारित, समय आधारित बारकोड स्कैनर के लिए समाधान के रूप में उपयोग किया जा सकता है।

उपयोग और सर्वोत्तम प्रथाओं के लिए ट्यूटोरियल, साथ ही विभिन्न बारकोड स्कैनर मॉडल और इससे निपटने के तरीके के बारे में चर्चा की गई। http://a.kabachnik.info/jquery-scannerdetection-tutorial.html

$(window).ready(function(){ 
 

 
\t //$("#bCode").scannerDetection(); 
 

 
\t console.log('all is well'); 
 
\t 
 
\t $(window).scannerDetection(); 
 
\t $(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

Vitall से समाधान केवल ठीक काम करता है अगर आप पहले से ही कम से कम एक कुंजी को दबाएँ। यदि आप पहले अक्षर नहीं को अनदेखा कर देंगे (यदि (यह ._timeout हैंडलर) झूठा लौटाता है और चार को जोड़ा नहीं जाएगा)।

आप तुरंत आप निम्नलिखित कोड का उपयोग कर सकते स्कैनिंग शुरू करना चाहते हैं:

var BarcodeScanerEvents = function() { 
 
\t this.initialize.apply(this, arguments); 
 
}; 
 

 
BarcodeScanerEvents.prototype = { 
 
\t initialize : function() { 
 
\t \t $(document).on({ 
 
\t \t \t keyup : $.proxy(this._keyup, this) 
 
\t \t }); 
 
\t }, 
 
\t _timeoutHandler : 0, 
 
\t _inputString : '', 
 
\t _keyup : function(e) { 
 
\t \t if (this._timeoutHandler) { 
 
\t \t \t clearTimeout(this._timeoutHandler); 
 
\t \t } 
 
\t \t this._inputString += String.fromCharCode(e.which); 
 

 
\t \t this._timeoutHandler = setTimeout($.proxy(function() { 
 
\t \t \t if (this._inputString.length <= 3) { 
 
\t \t \t \t this._inputString = ''; 
 
\t \t \t \t return; 
 
\t \t \t } 
 

 
\t \t \t $(document).trigger('onbarcodescaned', this._inputString); 
 

 
\t \t \t this._inputString = ''; 
 

 
\t \t }, this), 20); 
 
\t } 
 
};

संबंधित मुद्दे