A simple JavaScript utility to read barcode from devices emulating a fast keyboard.
# Yarn
yarn add simple-barcode-scanner
# NPM
npm install --save simple-barcode-scanner
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules
import BarcodeScanner from "simple-barcode-scanner";
// using CommonJS modules
const BarcodeScanner = require("simple-barcode-scanner");
The UMD build is also available on unpkg:
<script src="https://unpkg.com/simple-barcode-scanner"></script>
You can find the library on window.BarcodeScanner
.
import BarcodeScanner from "simple-barcode-scanner";
const scanner = BarcodeScanner();
// Add a listener
scanner.on((code, event) => {
event.preventDefault();
console.log(code);
});
// Remove the listener
scanner.off();
Creates a simple function to listen to barcode scanners
Parameters
-
Options
Object-
latency
Number Max time duration (in ms) between consecutive inputsdefault:
50
-
minLength
Number Min length of a valid barcodedefault:
3
-
element
HTMLElement The HTML element to attach the event listener todefault:
document
-
endKeys
Array<string> Array of keys indicating end of barcodeRefer Key Values | MDN
default:
["Enter"]
-
validKey
RegExp Regular expression to check for a valid key in barcodeRefer Key Values | MDN
default:
/^\w$/
-
Returns Scanner
-
Starts listening for barcode scans and add/replace the listener
Parameters
-
handler Function Function to call on completion of barcode scan
Recieves the scanned code and the keyboard event of the last input as the parameters
-
-
Stop listening for barcode scans and remove the listener
First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.
Development Start:
This project uses Typescript for development and yarn for dependency management.
Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If not, just open a new clear and descriptive issue.
Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
- Fork it!
- Clone your fork:
git clone https://github.com/<your-username>/simple-barcode-scanner
- Navigate to the newly cloned directory:
cd simple-barcode-scanner
- Create a new branch for the new feature:
git checkout -b my-new-feature
- Install the tools necessary for development:
yarn
- Make your changes.
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request with full remarks documenting your changes.