hs.keycodes
ModuleAccess information about the current keyboard layout and input sources, and respond to changes.
Reading the current layout
console.log("Layout: " + hs.keycodes.currentLayout())
console.log("Source ID: " + hs.keycodes.currentSourceID())
Key code mapping
// Look up a keycode by name
const code = hs.keycodes.map["a"] // e.g. 0 on ANSI US
// Look up a name by keycode
const name = hs.keycodes.map["0"] // e.g. "a"
Switching layouts
hs.keycodes.setLayout("British")
Watching for input source changes
hs.keycodes.addWatcher(() => {
console.log("Switched to: " + hs.keycodes.currentLayout())
})
Properties
hs.keycodes.map
{[key: string]: any}
A bidirectional mapping between key names and their macOS virtual key codes.
Entries exist for both directions: look up a name to get its integer keycode, or look
up a keycode (as a string) to get the key name. The map is rebuilt automatically
whenever the keyboard input source changes.
Methods
hs.keycodes.currentLayout() -> string
Returns the localized name of the current keyboard layout.
Uses the base keyboard layout, which is the underlying layout even when an input
method (such as a CJK input method) is also active.
Declaration
hs.keycodes.currentLayout() -> string
Returns
string
The display name of the active layout (e.g. `"U.S."`, `"British"`), or `null`.
Example
console.log("Layout: " + hs.keycodes.currentLayout())
hs.keycodes.currentMethod() -> string
Returns the localized name of the active input method, or `null` if none is active.
Input methods are distinct from keyboard layouts. They provide complex character
composition such as CJK input. Returns `null` when using a plain keyboard layout
with no input method overlay.
Declaration
hs.keycodes.currentMethod() -> string
Returns
string
The display name of the active input method (e.g. `"Hiragana"`), or `null`.
Example
const m = hs.keycodes.currentMethod()
if (m) console.log("Input method: " + m)
hs.keycodes.currentSourceID() -> string
Returns the reverse-DNS identifier of the currently selected keyboard input source.
Declaration
hs.keycodes.currentSourceID() -> string
Returns
string
A string such as `"com.apple.keylayout.US"`, or `null` if unavailable.
Example
console.log("Source ID: " + hs.keycodes.currentSourceID())
hs.keycodes.layouts() -> string[]
Returns the localized names of all currently enabled keyboard layouts.
Declaration
hs.keycodes.layouts() -> string[]
Returns
string[]
An array of layout name strings (e.g. `["U.S.", "British", "French"]`).
Example
hs.keycodes.layouts().forEach(l => console.log(l))
hs.keycodes.methods() -> string[]
Returns the localized names of all currently enabled input methods.
Declaration
hs.keycodes.methods() -> string[]
Returns
string[]
An array of input method name strings. May be empty if none are enabled.
Example
hs.keycodes.methods().forEach(m => console.log(m))
hs.keycodes.setLayout(layoutName) -> boolean
Switches the active keyboard layout to the one with the given localized name.
Use `layouts()` to enumerate valid names.
Declaration
hs.keycodes.setLayout(layoutName) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| layoutName | string | The localized name of the layout to activate (e.g. `"U.S."`). |
Returns
boolean
`true` if the layout was found and selected, `false` otherwise.
Example
if (!hs.keycodes.setLayout("U.S.")) console.log("Layout not found")
hs.keycodes.setMethod(methodName) -> boolean
Switches the active input method to the one with the given localized name.
Use `methods()` to enumerate valid names.
Declaration
hs.keycodes.setMethod(methodName) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| methodName | string | The localized name of the input method to activate. |
Returns
boolean
`true` if the method was found and selected, `false` otherwise.
Example
hs.keycodes.setMethod("Hiragana")
hs.keycodes.setSourceID(sourceID) -> boolean
Switches the active input source to the one with the given reverse-DNS identifier.
Use `currentSourceID()` to see the current value.
Declaration
hs.keycodes.setSourceID(sourceID) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| sourceID | string | The input source ID to activate (e.g. `"com.apple.keylayout.British"`). |
Returns
boolean
`true` if the source was found and selected, `false` otherwise.
Example
hs.keycodes.setSourceID("com.apple.keylayout.British")
hs.keycodes.addWatcher(listener) -> None
Registers a listener that fires whenever the keyboard input source changes.
The listener is called with no arguments. Read `currentLayout()`, `currentSourceID()`,
or `map` inside the callback to inspect the new state.
The OS subscription starts lazily on the first listener and is released automatically
when the last listener is removed via `removeWatcher`.
Declaration
hs.keycodes.addWatcher(listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| listener | function | A function called when the input source changes. |
Returns
None
Example
hs.keycodes.addWatcher(() => {
console.log("Now using: " + hs.keycodes.currentLayout())
})
hs.keycodes.removeWatcher(listener) -> None
Removes a previously registered input source change listener.
Declaration
hs.keycodes.removeWatcher(listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| listener | function | The function originally passed to `addWatcher`. |
Returns
None
Example
const handler = () => console.log("changed")
hs.keycodes.addWatcher(handler)
hs.keycodes.removeWatcher(handler)