npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@fimbul-works/barcode-scanner

v0.4.1

Published

Lightweight TypeScript library for detecting barcode scanner input in web applications by monitoring rapid keyboard event patterns

Readme

@fimbul-works/barcode-scanner

A lightweight, zero-dependency TypeScript library for detecting barcode scanner input in web applications.

npm version TypeScript

Features

  • 🎯 Zero Dependencies: Lightweight and self-contained
  • Fast Detection: Identifies barcode scans by monitoring rapid keystroke patterns (typically <10ms between characters)
  • 🔒 Type-Safe: Fully typed with TypeScript
  • 🎛️ Configurable: Customize timing, termination keys, and length constraints
  • 🐛 Debug Mode: Optional keystroke timing data for troubleshooting

Installation

npm install @fimbul-works/barcode-scanner
# or
yarn add @fimbul-works/barcode-scanner
# or
pnpm install @fimbul-works/barcode-scanner

Quick Start

import { BarcodeDetector } from '@fimbul-works/barcode-scanner';

// Create a detector with default settings
const detector = new BarcodeDetector();

// Listen for scans
const unsubscribe = detector.onScan((event) => {
  console.log('Barcode detected:', event.barcode);
  console.log('Scanned at:', new Date(event.timestamp));
});

// When done, cleanup
unsubscribe();
// or
detector.destroy();

How It Works

Barcode scanners act as "fast keyboard input devices" that send keydown events in rapid succession—much faster than human typing. This library detects those patterns by:

  1. Monitoring keystroke timing (default: <10ms between characters)
  2. Buffering characters until a termination key (default: Enter) or timeout
  3. Validating the length meets minimum requirements (default: 3 characters)
  4. Emitting a scan event with the detected barcode

Configuration Options

const detector = new BarcodeDetector({
  maxDelay: 10,              // Max ms between keystrokes (default: 10)
  terminationKey: 'Enter',   // Key that ends scan (default: 'Enter')
  minBarcodeLength: 3,       // Minimum characters (default: 3)
  maxBarcodeLength: 100,     // Maximum characters (default: 100)
  enableDebugEvents: false,  // Include timing data (default: false)
  preventDefault: false,     // Prevent default key behavior (default: false)
});

Advanced Usage

Debug Mode

Enable debug mode to see detailed keystroke timing information:

const detector = new BarcodeDetector({
  enableDebugEvents: true,
});

detector.onScan((event) => {
  console.log('Barcode:', event.barcode);
  console.log('Keystroke data:', event.keystrokes);
  // Example output:
  // [
  //   { key: '1', timestamp: 1234567890, delay: 0 },
  //   { key: '2', timestamp: 1234567895, delay: 5 },
  //   { key: '3', timestamp: 1234567900, delay: 5 },
  // ]
});

Multiple Listeners

You can attach multiple listeners to the same detector:

const detector = new BarcodeDetector();

const unsubscribe1 = detector.onScan((event) => {
  updateInventory(event.barcode);
});

const unsubscribe2 = detector.onScan((event) => {
  logToAudit(event.barcode);
});

// Cleanup individual listeners
unsubscribe1();
unsubscribe2();

Custom Termination Key

Some scanners can be configured to use different termination characters:

const detector = new BarcodeDetector({
  terminationKey: 'Tab', // Use Tab instead of Enter
});

Prevent Default Behavior

Prevent barcode scans from triggering form submissions or other default actions:

const detector = new BarcodeDetector({
  preventDefault: true,
});

Troubleshooting

Scans aren't being detected

  1. Check that your scanner is in keyboard emulation mode
  2. Verify the termination key matches your scanner's configuration
  3. Enable debug mode to inspect timing between keystrokes
  4. Adjust maxDelay if your scanner is slower than 10ms between characters

False positives (regular typing detected as scans)

  1. Increase minBarcodeLength to require longer inputs
  2. Decrease maxDelay to require faster typing (humans typically type slower than 50ms/character)

Scanner interferes with forms

Set preventDefault: true to stop barcode input from affecting the page.

License

MIT License - See LICENSE file for details.


Built with ⚡ by FimbulWorks