# hotkey-stack

> Hotkey Event Listener Stacks

Latest version **1.2.1** (published 2024-05-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install hotkey-stack
pnpm add hotkey-stack
yarn add hotkey-stack
bun add hotkey-stack
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2024-05-15 |
| First published | 2022-12-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 43.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Cameron Aziz |
| Maintainers | cameronaziz |
| Keywords | event listener, hotkey |

## Links

- npm: https://www.npmjs.com/package/hotkey-stack
- Repository: https://github.com/cameronaziz/hotkey-stack
- Homepage: https://github.com/cameronaziz/hotkey-stack#readme
- Issues: https://github.com/cameronaziz/hotkey-stack/issues
- npm.io page: https://npm.io/package/hotkey-stack

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.2.1 (latest) — 2024-05-15
- 1.2.0 — 2023-01-06
- 1.1.4 — 2023-01-05
- 1.1.3 — 2022-12-27
- 1.1.0 — 2022-12-27
- 1.0.0 — 2022-12-25
- 0.1.0 — 2022-12-25

## README

# Hotkey Stack

<img style="max-width: 400px; margin: 0 auto; display: block;" src="assets/images/stacks.png" />
A zero dependency package to create a stack of listeners for a hotkey.
This allows multiple registrations of the same hotkey.
By default, only the last registered listener will be called when it's hotkey is pressed.

## Setup

### NPM

```bash
npm install hotkey-stack
```

### Yarn

```bash
yarn add hotkey-stack
```

## Usage

### Basic

```typescript
import hs from 'hotkey-stack'

const callback = () => {
  console.log('Callback Called')
}

hs.add('a', callback)
```

#### Output when `A` key is pressed

```bash
Callback Called
```

### Multiple Listeners

```typescript
import hs from 'hotkey-stack'

const oneCallback = () => {
  console.log('One Callback Called')
}

const twoCallback = () => {
  console.log('Two Callback Called')
}

hs.add('a', oneCallback)
hs.add('a', twoCallback)
```

#### Output when `A` key is pressed

```bash
Two Callback Called
```

### Removing Listeners

```typescript
import hs from 'hotkey-stack'

const oneCallback = () => {
  console.log('One Callback Called')
}

const twoCallback = () => {
  console.log('Two Callback Called')
}

hs.add('a', oneCallback)
hs.add('a', twoCallback)
hs.pull(twoCallback)
```

#### Output when `A` key is pressed

```bash
One Callback Called
```

## Hotkey Config

Most API functions receive a `HotkeyConfig` which is either a `string` or `HotkeyComboConfig`.
The `HotkeyComboConfig` has the following properties:

|     Property      |   Type    | Required | Description                                                                                                            |
| :---------------: | :-------: | :------: | ---------------------------------------------------------------------------------------------------------------------- |
|       `key`       | `string`  |   Yes    | Key from `KeyboardEvent` to be listened to.                                                                            |
| `isMetaRequired`  | `boolean` |    No    | If `true`, meta key must be pressed. On Windows, this is the Windows Key (`⊞`). On Mac, this is the Command Key (`⌘`). |
| `isShiftRequired` | `boolean` |    No    | If `true`, the shift key must be pressed.                                                                              |
| `isCtrlRequired`  | `boolean` |    No    | If `true`, the ctrl key must be pressed.                                                                               |
|  `isAltRequired`  | `boolean` |    No    | If `true`, the alt key must be pressed.                                                                                |

## API

| Method  | Description                                                                                                                                                                                                                                                                                                   | Parameters                                            |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
| `add`   | Add a new listener into the stack for the provided hotkey. **Hotkeys are case insensitive.**                                                                                                                                                                                                                  | `hotkey: string, listener: Listener, symbol?: Symbol` |
| `pull`  | Remove a listener from the stack. To prevent all listeners from all hotkeys being removed, provide a second parameter of the hotkey that should be removed.                                                                                                                                                   | `listener: Listener, hotkey?: string`                 |
| `skip`  | Remove a listener from the stack but retain place stack. **This requires the symbol to be provided during `add`.** Failure to provide relational symbol will execute `pull` method. To prevent all listeners from all hotkeys being skipped, provide a second parameter of the hotkey that should be skipped. | `listener: Listener, hotkey?: string`                 |
| `cut`   | Moves the listener to the top of the stack. To prevent all listeners from all hotkeys cutting, provide a second parameter of the hotkey that should cut.                                                                                                                                                      | `listener: Listener, hotkey?: string`                 |
| `pause` | Pauses listening to all hotkeys.                                                                                                                                                                                                                                                                              |                                                       |
| `start` | Starts listening to all hotkeys. _This is automatic during instantiation and does not need to be called._                                                                                                                                                                                                     |                                                       |

## Advanced Usage

### Disabling Listener

The listener stack
Listeners can be temporaily

```typescript
import hs from 'hotkey-stack'

const oneCallback = () => {
  console.log('One Callback Called')
}

const twoCallback = () => {
  console.log('Two Callback Called')
}

hs.add('a', oneCallback)
hs.add('a', twoCallback)
hs.skip('a', twoCallback)
```

#### Output when `A` key is pressed

```bash
One Callback Called
```

### Disabling Listener and Adding Back

This example uses `skip`, and then `add`.
This **does not** require the listener to be the same reference.
The symbol passed to `add` will be used as the reference to retain the position.

```typescript
import hs from 'hotkey-stack'

const oneSymbol = Symbol()

const oneCallback = () => {
  console.log('One Callback Called')
}

const twoCallback = () => {
  console.log('Two Callback Called')
}

hs.add('a', oneCallback, oneSymbol)
hs.add('a', twoCallback)
hs.skip('a', oneCallback)
hs.add('a', oneCallback, oneSymbol)
```

#### Output when `A` key is pressed

```bash
Two Callback Called
```

### Prioritizing Listener

```typescript
import hs from 'hotkey-stack'

const oneCallback = () => {
  console.log('One Callback Called')
}

const twoCallback = () => {
  console.log('Two Callback Called')
}

hs.add('a', oneCallback)
hs.add('a', twoCallback)
hs.cut('a', oneCallback)
```

#### Output when `A` key is pressed

```bash
One Callback Called
```

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