GithubHelp home page GithubHelp logo

angular-hidscanner's Introduction

Angularjs HID Barcode Scanner

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.

Features

  • 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.

How to use

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

Dependencies:

Angularjs

Authors

Copyright::2014, Confiz. (http://www.confiz.com)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.