app.hotkey - Global Hotkey API
Registers and manages global keyboard shortcuts. Plugins can respond to shortcuts in the background.
Permission: L3 dangerous (requires Accessibility Permission)
Methods
app.hotkey.bind(mods, key, callback)
Registers a global shortcut.
Parameters:
mods(array) - Modifier key array:"cmd","shift","alt"/"option","ctrl"/"control","fn"key(string) - Key name:"a"-"z","0"-"9","f1"-"f12","space","return","tab","escape","left","right","up","down","delete","home","end","pageup","pagedown", etc.callback(function) - Callback function executed when the shortcut is triggered
Returns: string, error - Hotkey ID
-- Register Cmd+Shift+T
local id, err = app.hotkey.bind({"cmd", "shift"}, "t", function()
app.notification.show("Hotkey", "Cmd+Shift+T was pressed!")
end)
-- Register Ctrl+Alt+Space
app.hotkey.bind({"ctrl", "alt"}, "space", function()
app.log.info("Quick launch")
end)
app.hotkey.unbind(id)
Unregisters the specified shortcut.
Parameters:
id(string) - Hotkey ID
Returns: boolean, error
local ok, err = app.hotkey.unbind(id)
app.hotkey.unbindAll()
Unregisters all shortcuts for the current plugin.
Returns: boolean
app.hotkey.unbindAll()
app.hotkey.list()
Lists registered shortcuts for the current plugin.
Returns: array<table> - Each item contains:
id(string) - Hotkey IDkey(string) - Key namemods(array) - Modifier key arrayenabled(boolean) - Whether it is enabled
local hotkeys = app.hotkey.list()
for _, hk in ipairs(hotkeys) do
local mods = table.concat(hk.mods, "+")
app.log.info(hk.id .. ": " .. mods .. "+" .. hk.key .. " enabled=" .. tostring(hk.enabled))
end
app.hotkey.enable(id)
Enables a previously disabled shortcut.
Parameters:
id(string) - Hotkey ID
Returns: boolean, error
app.hotkey.disable(id)
Temporarily disables a shortcut (without removing the registration).
Parameters:
id(string) - Hotkey ID
Returns: boolean, error
-- Temporarily disable
app.hotkey.disable(id)
-- ... after some operations ...
-- Re-enable
app.hotkey.enable(id)
Description
- Each plugin can register up to 20 shortcuts
- Requires macOS Accessibility Permission (System Settings > Privacy & Security > Accessibility)
- The same key combination cannot be registered by multiple plugins
- Shortcuts are active across all windows and other applications
- Shortcuts are automatically unregistered when the plugin is unloaded
- Callbacks are invoked on the plugin execution queue (thread-safe)
- Shortcuts do not consume the original event; other applications can still receive it
Examples
Quick action panel
function MyPlugin:init()
-- Cmd+Shift+K to open quick actions
app.hotkey.bind({"cmd", "shift"}, "k", function()
local items = {
{text = "New File", id = "new"},
{text = "Open Terminal", id = "terminal"},
{text = "Copy Path", id = "copypath"},
}
local selected = app.chooser.show({
title = "Quick Actions",
items = items,
})
if selected then
self:executeAction(selected[1])
end
end)
end