Git Product home page Git Product logo

scanner's Introduction

Scanner

JavaScript scan-events for hardware barcode scanners.

How it works

Attempts to distinguish between regular input and scan input by measuring input spead, looking for certain suffix characters, etc. If a scan is detected, it triggers a custom JavaScript event called scan for the DOM element specified during initialization.

Quick Start

  • Not prublished to npm yet, so copy the sanner file to your project and import as module
  • Initialize scanner
import { Scanner } from "<relative path>/scanner/scanner";

// Create a scanner with optinal configs
scanner = new Scanner(options);

// Adds keydown listener for scanner
scanner.addEventListeners();

// Register event listener
document.addEventListener("scan", function (event) {
  const { parsed, scanned } = (event as CustomEvent).detail;
  console.log("processed scan value", parsed);
  console.log("raw scan value", scanned);
});

Options

The following options can be set when initializing Scanner:

Option Type Default Description
timer number 100 After first valid keydown, milliseconds after which scanning is considered.

Note:
- if prefixKeys exists, only then timer logic is used.
prefixKeys string[] [] List of possible keys that can be as prefix to the barcode.
suffixKeys string[] [] List of possible keys that can be as suffix to the barcode.
excludeListeningFromNodes string[] ["TEXTAREA", "INPUT"] Events from these nodes will be ignored.

Note:
- If suffixKeys exists. keyGap, prefixKeys logic is ignored.
keyGap number 40 Max time gap in milliseconds between each keydown events.=
onException (e: ScannerError) => void undefined Callback that return error or exception messages when encountered any.
onScan (values: ScannerValues) => void undefined Callback that return scanner values.
onKeyDetect (key: string, event: KeyboardEvent) => void undefined Callback after detecting a keyDown from expected nodes.
barcodeParser (value: string) => Map<string, string> undefined Parser logic to parse the scanner value.

Simulate Scan

// In project

import { Scanner } from "<relative path>/scanner/scanner";

// Create a scanner with optinal configs
scanner = new Scanner(options);

scanner.simulateScan("raw scanned value from scanner");

or

// In browser console

document.dispatchEvent(
  new KeyboardEvent("keydown", { key: "raw scanned value from scanner" })
);

scanner's People

Contributors

sriramrudraraju avatar

Watchers

 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.