https://github.com/moeki0/yubi
Yubi is a powerful library for managing keyboard shortcuts with ease and flexibility
https://github.com/moeki0/yubi
hotkeys keyboard shortcut
Last synced: 10 months ago
JSON representation
Yubi is a powerful library for managing keyboard shortcuts with ease and flexibility
- Host: GitHub
- URL: https://github.com/moeki0/yubi
- Owner: moeki0
- License: mit
- Created: 2024-11-01T10:13:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-02T00:10:54.000Z (over 1 year ago)
- Last Synced: 2025-07-31T13:37:13.544Z (11 months ago)
- Topics: hotkeys, keyboard, shortcut
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/yubi
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Yubi



```typescript
const yubi = new Yubi();
document.addEventListener("keydown", (e) => {
yubi.record(e);
if (yubi.match("cmd+k cmd+t")) {
// Code to be executed when the shortcut is matched
}
});
```
Yubi is a powerful library for managing keyboard shortcuts with ease and flexibility. Unlike conventional libraries, Yubi seamlessly integrates with addEventListener, allowing for intuitive and readable shortcut definitions.
It not only supports simultaneous key combinations but also recognizes sequential commands.
## Getting Started
To install Yubi, run:
```bash
npm install yubi
```
Then, import it into your project with:
```typescript
import { Yubi } from "yubi";
```
Using Yubi is straightforward: create a persistent instance and leverage it in event handlers.
```typescript
const yubi = new Yubi();
document.addEventListener("keydown", (e) => {
yubi.record(e); // Capture the event using the Yubi instance
if (yubi.match("cmd+s")) {
// Code to be executed when the shortcut is matched
}
});
```
For detecting sequences like pressing `cmd+k` followed by `cmd+t`, concatenate them with a space:
```typescript
document.addEventListener("keydown", (e) => {
yubi.record(e);
if (yubi.match("cmd+k cmd+t")) {
// Code to be executed when the sequence is matched
}
});
```
Customize the interval for sequential commands and the command delimiter upon instancing.
```typescript
const yubi = new Yubi({ delay: 500, delimiter: "-" });
// Default settings are { delay: 1000, delimiter: "+" }
```
## String Format
Yubi's hotkey string format allows defining combinations and sequences of keys to trigger specific actions. Key components and examples below illustrate this syntax.
### Basic Structure
1. **Modifiers and Keys**: Connect modifier keys (e.g., `cmd`, `ctrl`) to regular keys (e.g., `k`, `t`) using the specified delimiter (default: `+`).
2. **Sequences**: Use space to separate multiple hotkey combinations forming a sequence.
### Default Delimiter
- **Combination**: `cmd+k` (Press `command` and `k` together)
- **Sequence**: `cmd+k cmd+t` (Press `command` and `k`, then `command` and `t`)
### Custom Delimiter
Change the delimiter by configuring the Yubi instance. For example, with a `-` delimiter:
- **Combination**: `cmd-k`
- **Sequence**: `cmd-k cmd-t`
### Example Modifiers
Modifier Key | Keywords
-------------|--------
Alt | Alt, alt, Option, option, ⌥
Ctrl | Ctrl, ctrl, Control, control, ⌃
Shift | Shift, shift, ⇧
Meta | Meta, meta, Command, command, Cmd, cmd, ⌘
### Example Arrow Keys
Arrow Key | Keywords
--------------|---------
ArrowLeft | left, arrowLeft, ArrowLeft
ArrowRight | right, arrowRight, ArrowRight
ArrowUp | up, arrowUp, ArrowUp
ArrowDown | down, arrowDown, ArrowDown
By altering the `delimiter` option and structuring your hotkey strings according to the above format, you can craft personalized and versatile key command sequences with Yubi.
## Development
To set up the development environment, run:
```bash
npm install
npm test
```
## License
This library is distributed under the MIT license. See LICENSE for details.