Configure o prefixo e sufixo do leitor de código de barras (consulte o manual do fabricante)
- Prefixo: STX (início do texto, hex. 0x02)
- Sufixo: ETX (Fim do Texto, Hex. 0x03)
Use o seguinte plugin e exemplo para ler a saída do leitor de código de barras:
/* ========================================================================
* Barcode Reader: jquery.mmp.barcodereader.js
* https://github.com/mimopo/jquery.mmp.barcodereader
* http://mimopo.es
* ========================================================================
* Copyright 2014 - Miguel Montes Porras - @mimopoweb
* Licensed under WTFPL (http://www.wtfpl.net/)
* ======================================================================== */
(function( $ ) {
$.fn.mmpBarcodeReader = function() {
// Initialize buffer, it will contain the barcode scanner output
$(this).data('mmpBarcodeBuffer', '');
// Listen to barcode scanner output
$(this).keydown(function(e){
switch (e.which) {
// STX Prefix (Start of Text)
case 20:
$(this).trigger('start.mmp.barcodereader');
$(this).data('mmpBarcodeReading', true);
break;
// ETX Suffix (End of Text)
case 18:
$(this).trigger('end.mmp.barcodereader', $(this).data('mmpBarcodeBuffer'));
$(this).data('mmpBarcodeReading', false);
$(this).data('mmpBarcodeBuffer', '');
break;
// Regular char
default:
if ($(this).data('mmpBarcodeReading')){
$(this).trigger('char.mmp.barcodereader', String.fromCharCode(e.which));
$(this).data('mmpBarcodeBuffer', $(this).data('mmpBarcodeBuffer') + String.fromCharCode(e.which));
}
break;
}
});
// Sometimes the STX Prefix triggers alternately the keyup & keydown events. Let's fix it!
$(this).keyup(function(e){
if (e.which == 20){
$(this).trigger('start.mmp.barcodereader');
$(this).data('mmpBarcodeReading', true);
}
});
};
}( jQuery ));
Exemplo:
// Listen to all inputs inside the document element
$(document).ready(function(){
$(this).mmpBarcodeReader();
$(this).bind('start.mmp.barcodereader', function(e){
console.log('start', e);
});
$(this).bind('end.mmp.barcodereader', function(e, st){
console.log('end', e, st);
});
$(this).bind('char.mmp.barcodereader', function(e, ch){
console.log('char', e, ch);
});
});
Você tem o código em https://github.com/mimopo/jquery.mmp.barcodereader