API Docs

Access 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.
hs.keycodes.currentLayout() -> string
string
The display name of the active layout (e.g. `"U.S."`, `"British"`), or `null`.
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.
hs.keycodes.currentMethod() -> string
string
The display name of the active input method (e.g. `"Hiragana"`), or `null`.
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.
hs.keycodes.currentSourceID() -> string
string
A string such as `"com.apple.keylayout.US"`, or `null` if unavailable.
console.log("Source ID: " + hs.keycodes.currentSourceID())

hs.keycodes.layouts() -> string[]

Returns the localized names of all currently enabled keyboard layouts.
hs.keycodes.layouts() -> string[]
string[]
An array of layout name strings (e.g. `["U.S.", "British", "French"]`).
hs.keycodes.layouts().forEach(l => console.log(l))

hs.keycodes.methods() -> string[]

Returns the localized names of all currently enabled input methods.
hs.keycodes.methods() -> string[]
string[]
An array of input method name strings. May be empty if none are enabled.
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.
hs.keycodes.setLayout(layoutName) -> boolean
Name Type Description
layoutName string The localized name of the layout to activate (e.g. `"U.S."`).
boolean
`true` if the layout was found and selected, `false` otherwise.
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.
hs.keycodes.setMethod(methodName) -> boolean
Name Type Description
methodName string The localized name of the input method to activate.
boolean
`true` if the method was found and selected, `false` otherwise.
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.
hs.keycodes.setSourceID(sourceID) -> boolean
Name Type Description
sourceID string The input source ID to activate (e.g. `"com.apple.keylayout.British"`).
boolean
`true` if the source was found and selected, `false` otherwise.
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`.
hs.keycodes.addWatcher(listener) -> None
Name Type Description
listener function A function called when the input source changes.
None
hs.keycodes.addWatcher(() => {
    console.log("Now using: " + hs.keycodes.currentLayout())
})

hs.keycodes.removeWatcher(listener) -> None

Removes a previously registered input source change listener.
hs.keycodes.removeWatcher(listener) -> None
Name Type Description
listener function The function originally passed to `addWatcher`.
None
const handler = () => console.log("changed")
hs.keycodes.addWatcher(handler)
hs.keycodes.removeWatcher(handler)