# litkey

> Enjoyable keyboard shortcuts

Latest version **0.1.2** (published 2020-06-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install litkey
pnpm add litkey
yarn add litkey
bun add litkey
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2020-06-10 |
| First published | 2020-06-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Tobias Herber |
| Maintainers | tobihrbr |

## Links

- npm: https://www.npmjs.com/package/litkey
- Repository: https://github.com/varld/litkey
- Homepage: https://github.com/varld/litkey#readme
- Issues: https://github.com/varld/litkey/issues
- npm.io page: https://npm.io/package/litkey

## Dependencies (2)

- [is-hotkey](https://npm.io/package/is-hotkey.md) ^0.1.6
- [@testing-library/dom](https://npm.io/package/@testing-library/dom.md) ^7.11.0

## Recent versions

- 0.1.2 (latest) — 2020-06-10
- 0.1.1 — 2020-06-10
- 0.1.0-beta — 2020-06-09

## README

<h1 align="center">Litkey</h1>

<p align="center">🔥 Litkey makes keyboard shortcuts simple and enjoyable.</p>

## Install

```bash
# Using npm
npm install litkey

# Using yarn
yarn add litkey
```

## Usage

```typescript
import litkey from 'litkey';

// Add a global keyboard shortcut
litkey('mod+k', () => {
  // do something
});

// Add a keyboard shortcut to a specific element
litkey('mod+k', () => {
  // do something
}, myElement);
```

## Usage with React

```tsx
import { useShortcut } from 'litkey';

let Component = () => {
  let [clicked, setClicked] = useState(false);

  useShortcut('mod+a', () => {
    setClicked(true);
  });

  // You can also specify hook dependencies which will 
  // get passed on to the underlying useEffect
  useShortcut('mod+k', () => {
    setClicked(true);
  }, [/* react hook dependency list */]);

  // Using the fourth parameter, you can specify a
  // specific DOM element, in which the keyboard 
  // shortcut will be fired
  useShortcut('mod+k', () => {
    setClicked(true);
  }, [], myElement);

  return (
    <p>{ clicked ? 'clicked' : 'not clicked' }</p>
  );
};
```

## API

### `litkey(shortcut, handler, [context])`

The `litkey` function is the default export of litkey.

#### `shortcut: string | string[]`

`shortcut` is a `string` or an `array of strings`, which specify the key combinations which will fire the callback.

#### `handler: (event: KeyboardEvent) => any`

The `handler` is a callback function which will be called if the keyboard shortcut is pressed. 
It receives the `KeyboardEvent` as its first parameter.

#### `context?: HTMLElement`

The context is optional and can be used to specify the `HTMLElement`, in which litkey will listen for keyboard shortcuts.

### `useShortcut(shortcut, handler, [dependencies, [context]])`

#### `shortcut: string | string[]`

`shortcut` is a `string` or an `array of strings`, which specify the key combinations which will fire the callback.

#### `handler: (event: KeyboardEvent) => any`

The `handler` is a callback function which will be called if the keyboard shortcut is pressed. 
It receives the `KeyboardEvent` as its first parameter.

#### `dependencies: any[]`

`dependencies` is an optional array, which will be passed on directly to `useEffect` to serve as React hook dependencies.

#### `context?: HTMLElement`

`context` is optional and can be used to specify the `HTMLElement`, in which litkey will listen for keyboard shortcuts.

## License

MIT © [Tobias Herber](https://herber.space)

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