Skip to content

Latest commit

 

History

History
85 lines (64 loc) · 2.78 KB

File metadata and controls

85 lines (64 loc) · 2.78 KB

Scanner & Keyboard Input Handler

中文文档 (Chinese)

A robust, IE6-compatible JavaScript library for handling Barcode Scanner and Keyboard inputs in web applications.

Features

  • Dual Mode Detection: Automatically distinguishes between manual keyboard input and high-speed scanner input.
  • IE6 Compatible: Validated support for legacy browsers (IE6+), including json2.js integration for JSON parsing.
  • Event Separation: Uses keypress for accurate character capture (fixing legacy keydown issues with symbols like { or ") and keydown for control keys.
  • Buffering & validation: Built-in input buffering, length limits, and custom validation.
  • Throttle Control: Prevents duplicate submissions.
  • Rich Callbacks: Hooks for Scan Start, Input, Complete, Error, etc.

Installation

Simply include the scripts in your HTML file.

<!-- For IE6/7 support -->
<script src="json2.js"></script>
<!-- Main Library -->
<script src="main.js"></script>

Usage

1. Initialize

var scanner = new ScannerKeyboardInput({
    // Configuration
    maxCodeLength: 8,
    scanTimeout: 200, // ms
    
    // Callbacks
    onScanComplete: function(code) {
        console.log("Scanned Code:", code);
        // Do ajax post...
    },
    onKeyboardComplete: function(code) {
        console.log("Keyboard Input:", code);
    },
    onError: function(msg) {
        alert("Error: " + msg);
    }
});

// Automatically bind events to document
scanner.init();

// Or bind to a specific element
// scanner.init(document.getElementById('app'));

2. Configuration Options

| Option | Type | Default | Description |

Prefix Type Default Description
maxCodeLength Number 8 Max length for keyboard input buffering.
scanTimeout Number 2000 Timeout to reset scanner buffer (ms).
throttleInterval Number 3000 Time to wait before allowing duplicate submission (ms).
scanStartKeyCode Number 219 KeyCode for scanner start char (default: {).
scanEndKeyCode Number 221 KeyCode for scanner end char (default: }).
shouldSubmit Function null Custom validator. Return false to cancel submission.

3. Callbacks

  • onScanStart(): Triggered when scanner start sequence is detected.
  • onScanInput(val): Triggered on every char during scan.
  • onScanComplete(code): Triggered when scan is finished and parsed.
  • onKeyboardInput(char, loc, fullCode): Triggered on manual key press.
  • onKeyboardComplete(code): Triggered when manual input reaches maxCodeLength.
  • onError(msg): Triggered on JSON parse error or other failures.

Browser Support

  • Chrome, Firefox, Safari, Edge (Modern)
  • Internet Explorer 6, 7, 8, 9+

License

MIT