onScan.js is a lightweight, framework-agnostic JavaScript library designed to accurately detect and process input from hardware barcode scanners operating in either keyboard-wedge or clipboard (paste) mode. It intelligently distinguishes between normal keyboard input and high-speed scanner input by analyzing keypress timing, prefix/suffix characters, and other configurable parameters. Upon successful detection, it triggers a custom `scan` DOM event on the attached element or invokes a specified callback function, providing the scanned code and an optional quantity. The library is currently stable at version 1.5.2 and receives updates to enhance detection algorithms and add features like `captureEvents` (v1.5) for event priority. Its primary differentiation lies in its flexibility, robust detection logic, and comprehensive options for fine-tuning scanner behavior, making it suitable for a wide range of browser-based applications that require reliable barcode input without relying on specific frameworks.
npm install onscan.jsVerified import paths — ran on the pinned version, not inferred.
Initializes onScan.js on the document with common options, including a custom scan callback and a DOM event listener, then simulates a barcode scan to demonstrate functionality and output.
Consult your scanner's manual for its suffix characters (often Enter/Tab) and adjust `suffixKeyCodes`. Test `reactToPaste: true` for scanners that paste input instead of emulating keypresses.
Update your import statements to use `import onScan from 'onscan.js'` for ESM or `const onScan = require('onscan.js')` for CommonJS. If using a global script tag, ensure no module bundler is shadowing the global `onScan` object.When initializing, set `captureEvents: true` in the options object to ensure onScan.js receives and processes keyboard events with higher priority: `onScan.attachTo(document, { captureEvents: true, ... })`.If using a script tag, ensure `onscan.min.js` is loaded before your code. If using modules, use `import onScan from 'onscan.js'` for ESM or `const onScan = require('onscan.js')` for CommonJS.Ensure `captureEvents: true` (v1.5+) is set in your options. Verify `suffixKeyCodes` matches your scanner's termination character (e.g., `[13]` for Enter). Consider setting `preventDefault: true` in options if the library's default prevention isn't working.
Adjust options such as `suffixKeyCodes`, `minLength`, `timeBeforeScanTest`, and `minDelayBetweenKeys`. Check if `reactToPaste: true` is needed for clipboard-mode scanners. Use `onKeyDetect` callback to debug raw key codes being received.
No dependency data recorded yet.