This is a small module that can be used to read barcode scanner input. It works with scanners that act a keyboard devices. Its is inspired by http://www.deadosaurus.com/; a jquery version of the same implementation.
- HID barcode scanner reader.
- Runs in global scope.
- Uses angulajs broadcast system to avoid local scope bindings.
- Implemented as a service so that the initialization is in the control of the application developer.
factory('hidScanner', function($rootScope, $window, $timeout) {
return {
initialize : function() {
var chars = [];
var pressed = false;
angular.element($window).on('keypress', function(e) {
if (e.which >= 48 && e.which <= 57) {
chars.push(String.fromCharCode(e.which));
}
// console.log(e.which + ":" + chars.join("|"));
if (pressed == false) {
$timeout(function(){
if (chars.length >= 10) {
var barcode = chars.join("");
$rootScope.$broadcast("hidScanner::scanned", {barcode: barcode});
}
chars = [];
pressed = false;
},250);
}
pressed = true;
});
}
};
})
Above code defined an angular service which listens to windows keypress events. The line
$rootScope.$broadcast("hidScanner::scanned", {barcode: barcode});
broadcasts the received barcode input.
$timeout(function(){
if (chars.length >= 10) {
var barcode = chars.join("");
$rootScope.$broadcast("hidScanner::scanned", {barcode: barcode});
}
chars = [];
pressed = false;
},250);
Current timeout waits for 250 miliseconds. You can changes this value to match your scanner speed.
Add this module as dependency of your main app module:
angular.module('myAPp', ['angular-hidScanner'])
Pass the hidScanner service to you controller:
angular.controller('example', function ($scope, hIDScanner) {
hIDScanner.initialize(); // initialze the keypress event listner
});
- Furqan Razzaq (furqan.razzaq@confiz.com)
Copyright::2014, Confiz. (http://www.confiz.com)