2012-07-02 16 views

答えて

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

感謝を。しかし、どのようにバーコードが入力されたのかを検出できますか? 'onCange'イベントは、要素がフォーカスを失ったときにのみ発生しますが、挿入直後に挿入された値を知る必要があります。私はタイマーを作成し、定期的にテキストフィールドの値を確認することができますが、私は間違った解決策だと思う。 – MyTitle

+0

正確にあなたが入力をチェックしたいと思ったときにあなたの質問に言及しなかったので、私はonChangeを仮定しました。はい、おそらく、バーコードアプリケーションがページを操作するときに何らかの種類のカスタムイベントを発生させない限り、タイマーがおそらくそこの唯一の方法だと思います。 – Utkanos

1

入力ボックスで「onkeyup」イベントを使用できます。イベントがトリガされた場合は、「キーボードからの入力」と呼ぶことができます。まあ

18

私のバーコードスキャナMotorola LS1203がkeypressイベントを生成したので、私はUtkanosのソリューションを使用できません。

私のソリューションは、次のとおりです。

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

使用標準の接頭辞でHOMEキー)と接尾辞(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

すでに少なくとも1つのキーを打つ場合Vitallからソリューションは、正常に動作します。そうしないと、最初の文字は無視されます((this._timeoutHandler)がfalseを返し、charが付加されない場合)。

あなたはすぐに次のコードを使用することができますスキャンを開始する場合:

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

関連する問題