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

Key codes identify physical keys, while characters depend on the keyboard layout. map and names follow the current layout, so on Dvorak map["w"] is the key code of the key that types w there.

// 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.names[0]    // e.g. "a"

Switching layouts

hs.keycodes.setLayout("British")

Watching for input source changes

hs.keycodes.on('change', () => {
    console.log("Switched to: " + hs.keycodes.currentLayout())
})

Properties

hs.keycodes.map

{[key: string]: number}

A mapping from key names to macOS virtual key codes, following the current keyboard layout.

This is the same mapping hs.hotkey and hs.eventtap use to turn a key name into a key code. For the reverse direction, use names. The map is rebuilt automatically whenever the keyboard input source changes.

Names include:

  • Characters: every character the current layout types with no modifiers held. Characters the layout doesn't type (e.g. 1 on AZERTY) map to their US-ANSI key position.
  • Punctuation words: minus, equal, leftbracket, rightbracket, backslash, semicolon, quote, comma, period, slash, grave. Each is the key that types that character in the current layout, so comma and , are always the same key.
  • Function keys: f1–f20
  • Navigation: home, end, pageup, pagedown, left, right, up, down
  • Numpad: pad0–pad9, pad., pad+, pad-, pad*, pad/, pad=, padenter, padclear
  • Modifiers: cmd, rightcmd, shift, ctrl, alt, rightshift, rightalt, rightctrl, fn, capslock
  • Control: return, tab, space, delete, forwarddelete, escape, help
  • Media: volup, voldown, mute
  • JIS: yen, underscore, pad,, eisu, kana

hs.keycodes.names

{[key: string]: string}

A mapping from macOS virtual key codes to key names, following the current keyboard layout.

The reverse of map. Each key code maps to its fixed name if it has one (e.g. return, f1), otherwise to the character the current layout types on it. Key codes with neither are absent. JavaScript object keys are strings, so names[36] and names["36"] are the same.

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.

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.

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.

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.on(event, listener) -> None

Register a listener that fires whenever the keyboard input source changes. Read currentLayout(), currentSourceID(), or map inside the listener to inspect the new state.

hs.keycodes.on(event, listener) -> None
Name Type Description
event any The event to listen for (the only event this module emits)
listener any Called with no arguments when the input source changes
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.keycodes.on('change', () => console.log("Now using: " + hs.keycodes.currentLayout()))
} catch (err) {
    console.error(err.message)
}

hs.keycodes.off(event, listener) -> None

Remove a previously registered input source change listener.

hs.keycodes.off(event, listener) -> None
Name Type Description
event any The event the listener was registered for
listener any The function originally passed to on
None
const onChange = () => console.log("changed")
hs.keycodes.on('change', onChange)
// later…
hs.keycodes.off('change', onChange)

hs.keycodes.once(event, listener) -> None

Register a listener that fires at most once, the next time the keyboard input source changes.

hs.keycodes.once(event, listener) -> None
Name Type Description
event any The event to listen for
listener any Called once, then automatically removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.keycodes.once('change', () => console.log("First change detected"))
} catch (err) {
    console.error(err.message)
}