# bhk

> Linux desktop automation tool

Latest version **0.0.1** (published 2023-05-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-05-13 |
| First published | 2023-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 225.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dave Caruso |
| Maintainers | davecaruso |

## Links

- npm: https://www.npmjs.com/package/bhk
- npm.io page: https://npm.io/package/bhk

## Dependencies (1)

- [@paperdave/utils](https://npm.io/package/@paperdave/utils.md) ^1.6.1

## Recent versions

- 0.0.1 (latest) — 2023-05-13

## README

# BunHotKey

**Current Status**: Not usable, available for public experimentation.

A linux desktop automation tool providing programmable macros using Bun, a fast JavaScript runtime. It exposes an API to define macro keys, as well as many functions to interact with your system and the various applications I use.

> The demo below is not implemented and is more of a sketch of what I want to achieve

```ts
import { kb, ui, Keyboard } from "bhk";

// hotkeys on main keyboard, requires modifier
kb.macro("super + f1", () => {
  // `ui` contains live bindings to the current state of your desktop
  //    .activeWindow is a getter to fetch the active window
  //    .name is a getter to fetch the name of the active window
  console.log(`Macro activated in ${ui.activeWindow.name}`);

  // All getters are cached for one tick, meaning a second access will
  // not cause a second fetch, and is guaranteed to be the same value.
  console.log(`Macro activated in ${ui.activeWindow.proc.exe}`);

  // dispatch actions like typing a string
  ui.type("Hello world!");
});

// hook into external devices, for example adding an exclusive macro keyboard
// (linux version of Taren's 2nd keyboard stuff)
const kb2 = new Keyboard({ vendor: 0x1b1c, product: 0x1b13 });
kb2.macro("q", () => {
  // helper to focus or launch an application
  ui.focusOrLaunch("discord");

  // same as above but manual
  let window = ui.findWindow({ class: "discord" });
  if (window) {
    window.focus();
  } else {
    // launch returns a promise to the first window the process creates
    window = await ui.launch(["discord"]);
  }
});
```

## Reference

Any and all of this may be changed at any time.

### Desktop Automation API

BunHotKey exposes live bindings to your UI and window state.

- `ui`:
  - `activeWindow` -> `WindowRef`
  - `hoveredWindow` -> `WindowRef`
  - `mouse`
    - `pos` -> `{ x: number, y: number }`
    - `moveTo(x: number, y: number)`
    - `moveBy(x: number, y: number)`
    - `click(button: number)`
    - `down(button: number)`
    - `up(button: number)`
  - `findWindow(query: FindWindowOptions)` -> `?WindowRef`
- `i3`:
  - `async exec(command: string)`
  - `workspaces` -> `WorkspaceRef[]`
  - `workspace(name: string)` -> `WorkspaceRef`
  - `on(event, cb)`
    - `workspace`
    - `output`
    - `mode`
    - `window`
    - `barconfig_update`
    - `binding`
    - `shutdown`
    - `tick`

### `Device`

WIP, JS binding for reading to event devices (`/dev/input/event*`)

```ts
import { Device } from "bhk";

const device = new Device({
  vendor: 0x1b1c,
  product: 0x1b3d,
  grab: true,
});
device.on("key", (key) => {
  console.log(key);
});
```

### XKeyGrab

WIP, JS binding for X11's `XGrabKey`

## Development

BunHotKey is developed in Zig and TypeScript. Run `bun install` to install dependencies and build the library. Zig is installed through npm.

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