hs.hotkey
ModuleModule for creating and managing system-wide hotkeys
Types
This module provides the following types:
Properties
hs.hotkey.alertDuration
number
Duration in seconds for the on-screen toast shown when a hotkey with a
`message` set fires. Default is 1.
hs.hotkey.followsKeyboardLayout
boolean
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
`cmd` / `command` / `⌘`, `shift` / `⇧`, `alt` / `option` / `⌥`, `ctrl` / `control` / `⌃`.
through the current keyboard layout (see `hs.keycodes.map`), or a numeric virtual key code, which
always means the same physical key
Declaration
hs.hotkey.bind(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
Parameters
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings (e.g., `["cmd", "shift"]`). Supported names: |
| key | any | The key: a name or character (e.g. "a", "space", "return", "f1"), resolved |
| onPressed | function | A JavaScript function to call when the hotkey is pressed, or null for no callback |
| onReleased | function | A JavaScript function to call when the hotkey is released, or null/omitted for no callback |
| onRepeat | function | A JavaScript function to call repeatedly while the hotkey is held down, or null/omitted for no repeat |
Returns
HSHotkey
A hotkey object, or null if binding failed (including when none of the callbacks is a function — at least one is required)
Notes
The hotkey stays bound until you disable it, even if you don't keep a reference to it.
Example
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
Declaration
hs.hotkey.getKeyCodeMap() -> {[key: string]: number}
Returns
{[key: string]: number}
A dictionary mapping key names to numeric key codes (the same as `hs.keycodes.map`)
Example
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
Declaration
hs.hotkey.getModifierMap() -> {[key: string]: number}
Returns
{[key: string]: number}
A dictionary mapping modifier names to their numeric values
Example
console.log(hs.hotkey.getModifierMap())
hs.hotkey.create(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
Create a hotkey without enabling it
`cmd` / `command` / `⌘`, `shift` / `⇧`, `alt` / `option` / `⌥`, `ctrl` / `control` / `⌃`.
through the current keyboard layout (see `hs.keycodes.map`), or a numeric virtual key code, which
always means the same physical key
Declaration
hs.hotkey.create(mods, key, onPressed, onReleased, onRepeat) -> HSHotkey
Parameters
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings (e.g., `["cmd", "shift"]`). Supported names: |
| key | any | The key: a name or character (e.g. "a", "space", "return", "f1"), resolved |
| onPressed | function | A JavaScript function to call when the hotkey is pressed, or null for no callback |
| onReleased | function | A JavaScript function to call when the hotkey is released, or null/omitted for no callback |
| onRepeat | function | A JavaScript function to call repeatedly while the hotkey is held down, or null/omitted for no repeat |
Returns
HSHotkey
A hotkey object, or null if creation failed. Call `.enable()` to activate it.
Example
const hk = hs.hotkey.create(["cmd","shift"], "h", () => {
console.log("Hello!")
})
hk.enable()
hs.hotkey.getHotkeys() -> [[String: Any]]
Get a list of all currently-enabled hotkeys
Declaration
hs.hotkey.getHotkeys() -> [[String: Any]]
Returns
[[String: Any]]
An array of objects, each with `mods`, `key`, `keyCode`, `message` and `enabled` fields
Example
console.log(hs.hotkey.getHotkeys())
hs.hotkey.systemAssigned(mods, key) -> [String: Any]
Check whether macOS itself has already claimed a key combination (e.g. for Spotlight, screenshots, etc.)
Declaration
hs.hotkey.systemAssigned(mods, key) -> [String: Any]
Parameters
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | any | A key name or character, or a numeric virtual key code |
Returns
[String: Any]
An object with `keyCode`, `mods` and `enabled` fields if the combination is system-assigned, otherwise null
Example
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)
Declaration
hs.hotkey.assignable(mods, key) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | any | A key name or character, or a numeric virtual key code |
Returns
boolean
True if the combination can be bound, otherwise False
Example
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
Declaration
hs.hotkey.deleteAll(mods, key) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | any | A key name or character, or a numeric virtual key code |
Returns
None
Example
hs.hotkey.deleteAll(["cmd","shift"], "h")
hs.hotkey.disableAll(mods, key) -> None
Disable every hotkey currently bound to a key combination, without removing them
Declaration
hs.hotkey.disableAll(mods, key) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| mods | string[] | An array of modifier key strings |
| key | any | A key name or character, or a numeric virtual key code |
Returns
None
Example
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.
Declaration
hs.hotkey.bindSpec(spec) -> any
Parameters
| Name | Type | Description |
|---|---|---|
| spec | any | An object with the following fields: |
Returns
any
A hotkey object, or null if binding failed
Example
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
Declaration
hs.hotkey.createModal(mods, key) -> HSHotkeyModal
Parameters
| Name | Type | Description |
|---|---|---|
| mods | any | Modifier keys for the trigger hotkey (e.g. ["cmd", "shift"]). Defaults to an empty array (no modifiers) when omitted |
| key | any | 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 |
Returns
HSHotkeyModal
A modal object with bind(), enter(), exit(), destroy() methods, isActive property, and onEnter/onExit callbacks
Example
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.
Declaration
hs.hotkey.showHotkeys(mods, key) -> any
Parameters
| Name | Type | Description |
|---|---|---|
| mods | any | Modifier keys for the trigger hotkey (e.g. ["cmd", "shift"]) |
| key | any | Key name or numeric key code for the trigger hotkey (e.g. "/") |
Returns
any
A hotkey object
Example
hs.hotkey.showHotkeys(["cmd","shift"], "/")