# native-barcode-scanner

> A simple barcode scanner utility for node

Latest version **1.3.0** (published 2020-12-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install native-barcode-scanner
pnpm add native-barcode-scanner
yarn add native-barcode-scanner
bun add native-barcode-scanner
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2020-12-15 |
| First published | 2020-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 57.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | denyn_crawford |
| Maintainers | denyn_crawford |
| Keywords | barcode, scanner, driver |

## Links

- npm: https://www.npmjs.com/package/native-barcode-scanner
- Repository: https://github.com/denyncrawford/native-barcode-scanner
- Homepage: https://github.com/denyncrawford/native-barcode-scanner#readme
- Issues: https://github.com/denyncrawford/native-barcode-scanner/issues
- npm.io page: https://npm.io/package/native-barcode-scanner

## Dependencies (1)

- [gkm](https://npm.io/package/gkm.md) ^0.2.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2020-12-15
- 1.2.0 — 2020-12-15
- 1.1.0 — 2020-12-14
- 1.0.0 — 2020-12-14

## README

# Native Barcode Scanner 

It is a simple utility inspired by [simple-barcode-scanner](https://github.com/hadeeb/simple-barcode-scanner) but made as a native and global keyboard scanner driver for Node, this means that it doesn't depend on the browser and it listens even when no GUI/UI is focused. 

Most barcode scanners act like a keyboard, NBS handles this by listening for native events without using the DOM API. This is useful for scanning without focusing on any screen and works for multiple devices.

### Why?

I needed a way to use multiple scanners on a PC without depending on one window being foused. It is not viable / reliable to use on electron and browser runing on machines used by several users at time.

**Don't scare!** NBS is pretty simple too and it can be used with a sigle scanner on a simple app :D, but with the security that data is not getting lost anymore.

### How?

Barcode scanners are HID devices also, but there's one trick, they are **FORBIDEN** by some systems... Basically they doesn't allow HID connections for keyboards and mouses for security reasons. So while looking for a solution I found that `node-hid` is not that useful and we can't take a direct connection from the device. 

**So, how it is native?** Well, this is the bittersweet part: We use Java to capture native/global keys and keybindings events, and yeah... you have to install it :/ but it is not that hard :B (I mean, probably you have it already installed, you know... minecraft). This is because neither Node or Deno support native events for keyboards and mices. Anyway, it is incredibly fast as well. 

> I'm currently looking for a C++ solution like ioHook but its installation is a little bit cursed, and it crashes all the time (at least for me and my builds) if you want to contribute with a native way to listen to key events and pipe them to JS please make a PR :D.

##  📦 Install

```bash
npm install --save native-barcode-scanner
```

```javascript
// using ES6 modules
import BarcodeScanner from "native-barcode-scanner";

// using CommonJS modules
const BarcodeScanner = require("native-barcode-scanner");
```

## 🖥️ Usage

You can use NBS as a global listener or as a dedicated device listener.

### Basic (global):

> This will listens to al devices and will catch all codes from the multiple emitting devices. This is the way if you have just one device or if your device doesn't allow prefixing. 

```javascript
import BarcodeScanner from "native-barcode-scanner";

const options = {...foo}

const scanner = BarcodeScanner(options);

// Add a global listener
scanner.on('code', code => {
  console.log(code);
});

// Remove the listener
scanner.off();
```

### Dedicated device:

As I said before, some devices allow code prefixing functions and you can use it to scope the NBS events.

To do this, you have to prefix your device with your choosen device ID string and then specify it at `options.devicePrefix`.

> **Note**: NBS doesn't prefix your device, you must use one that does. Please read your device user guide. 

```javascript
import BarcodeScanner from "native-barcode-scanner";

const options = {
  devicePrefix: 'id1'
}

const scanner = BarcodeScanner(options);

// Add a global device scoped listener
scanner.on('code', code => {
  // This only works for the device(s) prefixed with id1
  console.log(code);
});

// Remove the listener
scanner.off();
```

> **Note**: We un-prefix the code for you ;) you can log it as clean as it is on the paper.

# 🧰 API

### BarcodeScanner
Creates an instance of Scanner to use the code events.

**Parameters**

- `Options` **Object**

  - `latency` **Number** Max time duration (in ms) between consecutive inputs

    _default: `50`_

  - `minLength` **Number** Min length of a valid barcode

    _default: `3`_

  - `endKeys` **string** Key name indicating end of barcode

    Refer [Key Values | MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)

    _default: `["Enter"]`_

  - `validKey` **RegExp** Regular expression to check for a valid key in barcode

    Refer [Key Values | MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)

    _default: `/^\w$/`_
  
  - `devicePrefix` **string** Prefix ID for device scoped events

    _default: `null`

Returns **Scanner**

### Scanner

- #### on

  Starts listening for barcode scans and add/replace the listener

  **Parameters**

  - eventName **string** Event string must be `code`

  - handler **Function** Function to call on completion of barcode scan

    _Recieves the scanned code of the last input as parametes_

- #### off
  Stop listening for barcode scans and remove the listener
  
# 🌐 Using on web

Please if you are on browser use [simple-barcode-scanner](https://github.com/hadeeb/simple-barcode-scanner)

If you want the multi-device update use my fork [@denyncrawford/simple-barcode-scanner](https://github.com/denyncrawford/simple-barcode-scanner/tree/device-instance)

# 👥 Credits

Developer: [denyncrawford](https://github.com/denyncrawford/)

This idea couln't be possible without [simple-barcode-scanner](https://github.com/hadeeb/simple-barcode-scanner), thanks.

# 🏗️ Contributing

1. Create an issue related to the problem or idea and check if it is viable
2. Fork it :D
3. Create a new branch with your changes.
4. Make a PR.

# 📜 License

MIT License

Copyright (c) 2020 Miguel Rangel

[See full licese](https://github.com/denyncrawford/native-barcode-scanner/tree/main/LICENSE)

---
_Source: https://npm.io/package/native-barcode-scanner · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
