Code Monkey home page Code Monkey logo

scan-detector's Introduction

Scan Detector

Detect barcode scanner and keyboard input events separately in your web or electronJS application with ease.

install

$ npm i scan-detector --save

Quick usage

const ScanDetector = require("scan-detector");

let detector = ScanDetector();

detector.attach();

document.addEventListener("scanner", (e) => {
  // handle scan code
});

document.addEventListener("keyboard", (e) => {
  // handle keyboard input
});

detector.detach();

Options

let sd = ScanDetector({ element: document, minCodeLength: 4 });
Option Type Default Description
element html element document html element to attach and listen for inputs
prefix array (char codes) [] these characters will be discarded
suffix array (char codes) [9, 13] possible end characters, will be discarded
avgCharTime number (ms) 30 average time between each scanner keydown event
minCodeLength number 6 min length of scanned code
customEncoder function(number keyCode) null will be called on each keydown event
onScanFinish function(string scanCode) null will be called when scan is finished
preventDefault bool false prevent default keydown behavior
stopPropagation bool false stops event propagation
debugMode bool false logs each keydown event in console

Methods

setOptions(options):

seta detector options.

getOptions():

returns detector options.

attach():

attach detector to the element, it will listen to keydown event in the element.

detach():

detach detector from element.

Example

const ScanDetector = require("scan-detector");

let input = document.getElementById("text_input");

let detector = ScanDetector({
  element: document,
  avgCharTime: 30,
  minCodeLength: 6,
  customEncoder: (event) => {
    return event.key;
  },
  onScanFinish: (code) => {
    return [...code.matchAll(/Alt([0-9]{3})/g)].reduce((t, v) => {
      return (t += String.fromCharCode(+v[1]));
    }, "");
  },
});

detector.attach();

document.addEventListener("scanner", (e) => {
  input.value = e.detail;
});

document.addEventListener("keyboard", (e) => {
  console.log(e);
});

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.