Skip to main content
Every recipe on this page works in software mode and with a Rebind Link, except Remap one device only, which needs hardware mode. Three building blocks cover them all:
  • rb.bind(key, fn, options) runs fn when key is pressed and takes that key, so your computer never sees it. Rebind intercepts only the keys you bind. rb.bind.remap(from, to) is the one-line key-to-key form.
  • rb.on("down", fn) / rb.on("up", fn) see every key. Return false to block a key, anything else to let it through. A script with one of these handlers captures the whole keyboard, so use them only when the logic depends on keys you cannot list up front. See Input.
  • rb.input.is_down(key) tells you whether a key is held right now, which is how chords, holds, and layers decide what to do.
Press Left Ctrl + Left Alt + K (Left Control + Left Option + K on macOS) to stop every script and release every held key and button. In hardware mode, press it on a keyboard you attached in Peripherals. See the kill switch.

Single-key remap

rb.bind.remap takes the key you press and the key to send in its place. Caps Lock becomes Escape; your real Escape is untouched.
A remap returns a handle, so it can be switched off and on while the script runs. Here F8 flips it, and handle.enabled reports the state.

A key that sends a shortcut

A bind can send any chord. rb.hid.combo presses the keys left to right and releases them right to left in one call, so it works directly in a callback. The when option limits the bind to a condition; when it returns false, the key passes through as usual. Here F1 reopens the last closed tab, but only while a Chrome window is focused.
A modifier you are holding is added to the chord; see rb.hid. To scope a whole script to some applications with the modeline instead, see Per-app shortcuts.

Chords

A chord fires one action when several keys are held together. Printable keys must be held back: if J passed through at once, it would already be typed by the time K completes the chord. This script holds J and K until release, sends Escape when both are down, and types the single key on release when no chord formed.
The tradeoff: a lone J or K appears on release instead of press, and holding it does not auto-repeat. Typing J and K so that both are down at once sends Escape.

Tap or hold

One key, two jobs: tap Space for a space, hold it and press H, J, K, or L for the arrow keys. The script withholds Space until it knows which one you meant.
used stops a stray space after you used the hold, and TAP_MS keeps a long, idle hold from typing a late one. Raise TAP_MS if quick taps go missing.

Tap for a key, hold for a modifier

Tap Caps Lock for Escape; hold it and press another key for Ctrl. An input_down observer notes any other key pressed during the hold. Observers see every key, bound ones included, and take no exclusive capture.
Ctrl goes down as soon as Caps Lock does, so Ctrl+click works during the hold. A tap sends a bare Ctrl press and release before Escape.

Hold-to-access layer

A layer is a hold scaled up: hold one key to turn a block of keys into something else, release it to return to normal. The mappings live in a table, so adding a key is one line.
Each layer key holds its target down until you release it. Every other key, and every layer key while the layer key is up, behaves normally. To limit the layer to one application, add a process= or window= line to the modeline.

Remap one device only

rb.bind.remap answers to every device unless its key is a pinned key. To remap keys on one keyboard only, use the layer pattern above with device in place of when. Give the keyboard a nickname in Peripherals first; see Attach your devices. This script turns Caps Lock and Right Alt into Ctrl on a keyboard nicknamed compact.
Your other keyboards keep their own Caps Lock and Right Alt. Identical keyboards without serial numbers may share an id, and with it a nickname; see Devices. If the script also binds or remaps one of these keys for every device, the device binds run first whatever the creation order; see bind.

Last pressed wins

For a pair of opposing keys, such as left and right, holding both is ambiguous. This script resolves it: while both are held, the most recently pressed one is active, and releasing it hands control back to the other.
Pressing either key releases the other’s output and sends its own. Releasing the newer key restores the older one if it is still held, with no extra tap.