1.0.4 • Published 2 years ago

scan-detector v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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 });
OptionTypeDefaultDescription
elementhtml elementdocumenthtml element to attach and listen for inputs
prefixarray (char codes)[]these characters will be discarded
suffixarray (char codes)9, 13possible end characters, will be discarded
avgCharTimenumber (ms)30average time between each scanner keydown event
minCodeLengthnumber6min length of scanned code
customEncoderfunction(number keyCode)nullwill be called on each keydown event
onScanFinishfunction(string scanCode)nullwill be called when scan is finished
preventDefaultboolfalseprevent default keydown behavior
stopPropagationboolfalsestops event propagation
debugModeboolfalselogs 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);
});
1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago