hs.hotkey
ModuleModule for creating and managing system-wide hotkeys
Types
This module provides the following types:
Properties
hs.hotkey.alertDuration
Duration in seconds for the on-screen toast shown when a hotkey with a
message set fires. Default is 1.
hs.hotkey.followsKeyboardLayout
Whether hotkeys bound with a key name move to a different key when the keyboard layout changes, so that they stay on the key that types that character. Default is true.
For example, a hotkey bound to "w" is on the key US-ANSI calls W while a US layout is
active, and moves to the key US-ANSI calls , when you switch to Dvorak, since that's where
Dvorak types w. Hotkeys bound with a numeric key code never move.
When false, a hotkey stays on whichever key its name resolved to when it was created, as in Hammerspoon v1. Setting this back to true moves existing hotkeys to the current layout. If the new layout doesn't type a hotkey's character and the character has no US-ANSI position either, the hotkey stays where it is and a warning is logged.
Avoid binding some hotkeys by key name and others by numeric key code with the same
modifiers if you switch layouts. A hotkey that moves onto a key code another hotkey already
holds can't be registered there, so it's disabled with an error logged, and it stays
disabled until you call its enable() again, even after switching back.
Methods
hs.hotkey.bind(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
Bind a hotkey
hs.hotkey.bind(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings (e.g., ["cmd", "shift"]). Supported names: cmd / command / ⌘, shift / ⇧, alt / option / ⌥, ctrl / control / ⌃. |
| key | string | number | The key: a name or character (e.g. "a", "space", "return", "f1"), resolved through the current keyboard layout (see hs.keycodes.map), or a numeric virtual key code, which always means the same physical key |
| onPressed | (() => void) | null | A JavaScript function to call when the hotkey is pressed, or null for no callback |
| onReleased optional | (() => void) | null | A JavaScript function to call when the hotkey is released, or null/omitted for no callback |
| onRepeat optional | (() => void) | null | A JavaScript function to call repeatedly while the hotkey is held down, or null/omitted for no repeat |
hs.hotkey.bind(["cmd","shift"], "h", () => {
console.log("Hello!")
}, null, () => console.log("still held"))
hs.hotkey.getKeyCodeMap() -> {[key: string]: number}
Get the mapping of key names to key codes that hotkeys use, following the current keyboard layout
hs.hotkey.getKeyCodeMap() -> {[key: string]: number}
hs.keycodes.map)console.log(hs.hotkey.getKeyCodeMap()["w"]) // 13 on US, 43 on Dvorak
hs.hotkey.getModifierMap() -> {[key: string]: number}
Get the mapping of modifier names to modifier flags
hs.hotkey.getModifierMap() -> {[key: string]: number}
console.log(hs.hotkey.getModifierMap())
hs.hotkey.create(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
Create a hotkey without enabling it
hs.hotkey.create(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings (e.g., ["cmd", "shift"]). Supported names: cmd / command / ⌘, shift / ⇧, alt / option / ⌥, ctrl / control / ⌃. |
| key | string | number | The key: a name or character (e.g. "a", "space", "return", "f1"), resolved through the current keyboard layout (see hs.keycodes.map), or a numeric virtual key code, which always means the same physical key |
| onPressed | (() => void) | null | A JavaScript function to call when the hotkey is pressed, or null for no callback |
| onReleased optional | (() => void) | null | A JavaScript function to call when the hotkey is released, or null/omitted for no callback |
| onRepeat optional | (() => void) | null | A JavaScript function to call repeatedly while the hotkey is held down, or null/omitted for no repeat |
.enable() to activate it.const hk = hs.hotkey.create(["cmd","shift"], "h", () => {
console.log("Hello!")
})
hk.enable()
hs.hotkey.getHotkeys() -> (object)[]
Get a list of all currently-enabled hotkeys
hs.hotkey.getHotkeys() -> (object)[]
| Field | Type | Description |
|---|---|---|
| mods | string[] | The hotkey's modifier keys |
| key | string | number | The key, as it was given when the hotkey was created |
| keyCode | number | The key's virtual key code |
| message | string | null | The hotkey's message |
| enabled | boolean | Whether the hotkey is enabled |
console.log(hs.hotkey.getHotkeys())
hs.hotkey.systemAssigned(mods, key) -> object | null
Check whether macOS itself has already claimed a key combination (e.g. for Spotlight, screenshots, etc.)
hs.hotkey.systemAssigned(mods, key) -> object | null
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | string | number | A key name or character, or a numeric virtual key code |
| Field | Type | Description |
|---|---|---|
| keyCode | number | The shortcut's virtual key code |
| mods | number | The shortcut's modifier keys, as Carbon modifier flags |
| enabled | boolean | Whether the shortcut is enabled |
console.log(hs.hotkey.systemAssigned(["cmd","space"], "space"))
hs.hotkey.assignable(mods, key) -> boolean
Check whether a key combination is available to be bound (i.e. not already claimed by macOS)
hs.hotkey.assignable(mods, key) -> boolean
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | string | number | A key name or character, or a numeric virtual key code |
console.log(hs.hotkey.assignable(["cmd","shift"], "h"))
hs.hotkey.deleteAll(mods, key) -> None
Disable and remove every hotkey currently bound to a key combination
hs.hotkey.deleteAll(mods, key) -> None
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | string | number | A key name or character, or a numeric virtual key code |
hs.hotkey.deleteAll(["cmd","shift"], "h")
hs.hotkey.disableAll(mods, key) -> None
Disable every hotkey currently bound to a key combination, without removing them
hs.hotkey.disableAll(mods, key) -> None
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | string | number | A key name or character, or a numeric virtual key code |
hs.hotkey.disableAll(["cmd","shift"], "h")
hs.hotkey.bindSpec(spec) -> any
Bind a hotkey from a single options object. Like hs.hotkey.bind()/create(), but also
accepts a message and a repeat callback. message is available on any hotkey (not
just ones created via bindSpec()) by setting .message directly on the returned object;
see hs.hotkey's message property for exactly when it is shown.
hs.hotkey.bindSpec(spec) -> any
| Name | Type | Description |
|---|---|---|
| spec | object | The hotkey to bind |
| mods | string[] | Modifier keys (e.g. ["cmd", "shift"]) |
| key | string | number | Key name or character (e.g. "h"), or a numeric key code |
| message optional | string | A description shown as a toast when the hotkey fires |
| pressed optional | (() => void) | null | Called when the hotkey is pressed |
| released optional | (() => void) | null | Called when the hotkey is released |
| repeat optional | (() => void) | null | Called repeatedly while the hotkey is held down |
hs.hotkey.bindSpec({
mods: ["cmd"], key: "space", message: "Spotlight-like",
pressed: () => console.log("pressed")
})
hs.hotkey.createModal(mods, key) -> HSHotkeyModal
Create a new modal hotkey group, optionally entered via a trigger key combination
hs.hotkey.createModal(mods, key) -> HSHotkeyModal
| Name | Type | Description |
|---|---|---|
| mods optional | string[] | Modifier keys for the trigger hotkey (e.g. ["cmd", "shift"]). Defaults to an empty array (no modifiers) when omitted |
| key optional | string | number | Key name or numeric key code for the trigger hotkey (e.g. "h"). Omit it, or pass null or an empty string, for a modal with no trigger |
const m = hs.hotkey.createModal(['cmd'], 'h')
m.bind(['shift'], 'j', () => console.log('shift-j pressed'))
m.onEnter = () => console.log('modal entered')
m.onExit = () => console.log('modal exited')
m.bind([], 'escape', () => m.exit())
// A modal with no trigger, entered and exited from your own code
const n = hs.hotkey.createModal()
n.bind([], 'escape', () => n.exit())
n.enter()
hs.hotkey.showHotkeys(mods, key) -> any
Create and enable a hotkey that, while held down, displays a list of all currently enabled hotkeys (and their messages, if any) as an on-screen toast.
hs.hotkey.showHotkeys(mods, key) -> any
| Name | Type | Description |
|---|---|---|
| mods | any | Modifier keys for the trigger hotkey (e.g. ["cmd", "shift"]) |
| key | string | number | Key name or numeric key code for the trigger hotkey (e.g. "/") |
hs.hotkey.showHotkeys(["cmd","shift"], "/")