Git Product home page Git Product logo

barcode-scanner.js's Introduction

barcode-scanner.js

Javascript library to handle and identify data (keystrokes) entered by a barcode scanner. Barcode Scanner

Demo / Try it

You can try the library and if you scanner does work with it here: DEMO

FAQ

Please check if you question was already asked/solved here: Github Issues

How it works

A barcode scanner works like a keyboard, when you scan a specific barcode, the barcode scanner device types the text version of the barcode like a keyboard, but it types so fast (<50ms between keystrokes) that you can identify input that is done with a barcode scanner and handle it in a special way.

Default usage

A barcode scanner input is pasted to the last input/textarea with an 'data-barcode-scanner-target' attribute found in the dom and submits the closest form.

  <form>
    <input data-barcode-scanner-target type='text'></input>
  </form>
  • If scanner scans 'IT1232'
  • barcode-scanner.js inserts 'IT1232' into the input field
  • Afterwards it submits the surrounding form

Behaviour when user focuses an input field

If an input or textarea is focused (clicked) by the user, the barcode scanner input is always inserted to the focused input/textarea and the surrounding form is submitted.

  <form>
    <input name='first' type='text'></input>
    <input name='second' data-barcode-scanner-target type='text'></input>
  </form>
  • User focuses (clicks) the input with the name 'first'
  • Scanner scans 'IT1232'
  • barcode-scanner.js inserts 'IT1232' to the input with name "first" (as its focused)
  • Afterwards it submits the surrounding form

Disable auto submit

The auto submit of the surrounding form can be disabled.

  <form data-prevent-barcode-scanner-submit>
    <input data-barcode-scanner-target type='text'></input>
  </form>

or

  <form>
    <input data-barcode-scanner-target data-prevent-barcode-scanner-submit type='text'></input>
  </form>
  • If scanner scans 'IT1232'
  • barcode-scanner.js inserts 'IT1232' into the input field
  • Afterwards it does NOT submit the surrounding form, as it's disabled

Custom actions / More complex scenarios

In case you have more complex requirements, like multiple input fields where something has to decide in which input field the code has to be entered, custom actions is what you can use to solve it.

  BarcodeScanner.addAction("c (id)", function(id) {
    console.log(this); // the current input/textarea that is the barcode target
    console.log(id);  // 2121232
  });
  • If scanner scans 'c 2121232'
  • The registered action will be performed (as it matches the registered pattern)
  • No form is getting submitted, no chars will be added to any input field

Structured Barcodes / Custom actions

If your barcode contains structured information, like which type of item is it etc., it's recommended to structure you barcode. This would allow this library to identify each part of the barcode. Like 'c 123 buy 5' where 'c' stands for custom action, '123' is the id of the item, buy the action you want to perform and '5' the amount.

  BarcodeScanner.addAction("c (id) (action) (amount)", function(id, action, amount) {
    console.log(this);    // the current input/textarea that is the barcode target
    console.log(id);      // 2121232
    console.log(action);  // print
    console.log(amount);  // 2
  });
  • If scanner scans "c 2121232 print 2"
  • The registered action will be performed (as it matches the registered pattern)
  • No form is getting submitted, no chars will be added to any input field

Tests

Jasmine Tests

barcode-scanner.js's People

Watchers

James Cloos avatar Sergio Araya Villalobos avatar

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.