Returns what characters are produced by pressing keys with different modifiers on the current system keyboard layout.
- On Debian-based Linux:
sudo apt-get install libx11-dev libxkbfile-dev - On Red Hat-based Linux:
sudo yum install libx11-devel.x86_64 libxkbfile-devel.x86_64 # or .i686 - On SUSE-based Linux:
sudo zypper install libX11-devel libxkbfile-devel - On FreeBSD:
sudo pkg install libX11 libxkbfile
npm install native-keymapvar keymap = require('native-keymap');
console.log(keymap.getKeyMap());Example output when using standard US keyboard layout (on Windows):
[
...
Space: { vkey: 'VK_SPACE', value: ' ', withShift: ' ', withAltGr: '', withShiftAltGr: '' },
Minus: { vkey: 'VK_OEM_MINUS', value: '-', withShift: '_', withAltGr: '', withShiftAltGr: '' },
Equal: { vkey: 'VK_OEM_PLUS', value: '=', withShift: '+', withAltGr: '', withShiftAltGr: '' },
BracketLeft: { vkey: 'VK_OEM_4', value: '[', withShift: '{', withAltGr: '', withShiftAltGr: '' },
BracketRight: { vkey: 'VK_OEM_6', value: ']', withShift: '}', withAltGr: '', withShiftAltGr: '' },
Backslash: { vkey: 'VK_OEM_5', value: '\\', withShift: '|', withAltGr: '', withShiftAltGr: '' },
Semicolon: { vkey: 'VK_OEM_1', value: ';', withShift: ':', withAltGr: '', withShiftAltGr: '' },
Quote: { vkey: 'VK_OEM_7', value: '\'', withShift: '"', withAltGr: '', withShiftAltGr: '' },
Backquote: { vkey: 'VK_OEM_3', value: '`', withShift: '~', withAltGr: '', withShiftAltGr: '' },
Comma: { vkey: 'VK_OEM_COMMA', value: ',', withShift: '<', withAltGr: '', withShiftAltGr: '' },
Period: { vkey: 'VK_OEM_PERIOD', value: '.', withShift: '>', withAltGr: '', withShiftAltGr: '' },
Slash: { vkey: 'VK_OEM_2', value: '/', withShift: '?', withAltGr: '', withShiftAltGr: '' },
...
]
Example output when using German (Swiss) keyboard layout (on Windows):
[
...
Space: { vkey: 'VK_SPACE', value: ' ', withShift: ' ', withAltGr: '', withShiftAltGr: '' },
Minus: { vkey: 'VK_OEM_4', value: '\'', withShift: '?', withAltGr: '´', withShiftAltGr: '' },
Equal: { vkey: 'VK_OEM_6', value: '^', withShift: '`', withAltGr: '~', withShiftAltGr: '' },
BracketLeft: { vkey: 'VK_OEM_1', value: 'ü', withShift: 'è', withAltGr: '[', withShiftAltGr: '' },
BracketRight: { vkey: 'VK_OEM_3', value: '¨', withShift: '!', withAltGr: ']', withShiftAltGr: '' },
Backslash: { vkey: 'VK_OEM_8', value: '$', withShift: '£', withAltGr: '}', withShiftAltGr: '' },
Semicolon: { vkey: 'VK_OEM_7', value: 'ö', withShift: 'é', withAltGr: '', withShiftAltGr: '' },
Quote: { vkey: 'VK_OEM_5', value: 'ä', withShift: 'à', withAltGr: '{', withShiftAltGr: '' },
Backquote: { vkey: 'VK_OEM_2', value: '§', withShift: '°', withAltGr: '', withShiftAltGr: '' },
Comma: { vkey: 'VK_OEM_COMMA', value: ',', withShift: ';', withAltGr: '', withShiftAltGr: '' },
Period: { vkey: 'VK_OEM_PERIOD', value: '.', withShift: ':', withAltGr: '', withShiftAltGr: '' },
Slash: { vkey: 'VK_OEM_MINUS', value: '-', withShift: '_', withAltGr: '', withShiftAltGr: '' },
...
]
- linux (X11 and Wayland)
- windows
- mac
- freebsd
On Linux the module reads the keyboard layout from the Wayland compositor when WAYLAND_DISPLAY is set and a compositor accepts the connection, and from the X server otherwise. The Wayland backend binds wl_seat, receives the XKB keymap the compositor sends with wl_keyboard.keymap, and resolves it with libxkbcommon, so it works on Wayland-only sessions and reports the layout the compositor actually uses, also when Electron runs as an XWayland client. A Wayland client without a focused surface doesn't learn which layout of the keymap is active, so a keymap with several layouts (e.g. us,ru) is read from the X server when one can be reached: Electron apps such as VS Code run under XWayland unless they are started with --ozone-platform=wayland, and XWayland tracks the active layout while one of their windows is focused. onDidChangeKeyboardLayout listens to both.
- Set
NATIVE_KEYMAP_BACKEND=x11to always use the X server by default, orNATIVE_KEYMAP_BACKEND=waylandto never fall back to it. The module reads it once per process, the first time it needs the default, so set it before then. - The Wayland backend needs nothing to build: the module loads
libwayland-client.so.0andlibxkbcommon.so.0the first time it reads the compositor, and reads the X server when they can't be loaded. It works with libwayland-client releases before 1.20 too. Build withNATIVE_KEYMAP_WAYLAND=0to leave the backend out: such a build still honorsNATIVE_KEYMAP_BACKEND=wayland, and then reads nothing by default (getKeyMap()gives{},getCurrentKeyboardLayout()givesnull,getKeyboardBackends().defaultis'wayland'), so the application can tell that it doesn't know the layout. - With
NATIVE_KEYMAP_BACKEND=wayland, or without an X server, the active layout of a keymap with several layouts is unknown:getCurrentKeyboardLayout().groupis always0and switching between its layouts fires noonDidChangeKeyboardLayoutevent. Changing the keymap itself does fire it. - When the connection to the compositor is lost,
onDidChangeKeyboardLayoutfires and the module reads the X server until it connects again; it tries every second and fires again once connected. A compositor that doesn't answer within 2 seconds while the module picks the default, for example under load at startup, stays the default in the same way. - When the X server goes away, for example when XWayland is restarted,
onDidChangeKeyboardLayoutfires too, and the module connects to it again every second and fires once connected. Xlib exits the process when it finds the connection lost itself: libX11 1.7 and later let the module prevent that, while with an older libX11 the module only survives when it notices first. - The layout names come from the keymap's
xkb_symbolssection, such aspc+us+ru:2+inet(evdev). libxkbcommon 1.8 and older name it(unnamed), and 1.9 and newer escape the name (pc_us_ru_2_inet(evdev)); then the layouts are looked up by their group names' descriptions in the XKB registry (evdev.xml), and a layout that isn't in the registry is reported as empty.modelandoptionscome fromXKB_DEFAULT_MODELandXKB_DEFAULT_OPTIONS, as the keymap doesn't carry them.
getKeyMap and getCurrentKeyboardLayout take an optional backend, 'x11' or 'wayland', to read that one instead of the default: 'wayland' reads the compositor even when the default is the X server, and never falls back to the X server. A backend that can't be read gives {} and null, and so does 'wayland' when WAYLAND_DISPLAY is unset or empty, when the module is built without it, or when libwayland-client or libxkbcommon can't be loaded. The module only connects to the compositor WAYLAND_DISPLAY names, never to libwayland's fallback wayland-0, which can belong to another session. getKeyboardBackends() tells which backends can be read and which one is the default, so an application can read both and decide, for example by the platform its windows use. It returns undefined on Windows and macOS, which ignore the backend argument. On Linux, any other value logs a TypeError and gives an empty result, as other errors in the getters do.
const keymap = require('native-keymap');
const backends = keymap.getKeyboardBackends(); // e.g. { x11: true, wayland: true, default: 'wayland' }
if (backends && backends.x11 && backends.wayland) {
const fromCompositor = keymap.getCurrentKeyboardLayout('wayland');
const fromXServer = keymap.getCurrentKeyboardLayout('x11');
}onDidChangeKeyboardLayout follows the backends the default reads: with NATIVE_KEYMAP_BACKEND=x11, or without WAYLAND_DISPLAY, it doesn't listen to the compositor, and with NATIVE_KEYMAP_BACKEND=wayland it doesn't listen to the X server.
- only tested from the Electron Main process
- on Linux with libX11 1.7 or later, the X11 listener installs a process-wide Xlib IO error handler (
XSetIOErrorHandler) so that losing the X server doesn't exit the process. It passes displays it doesn't own to the handler installed before it. If other code replaces it later, losing the X server exits the process, as without this module.
npm install -g node-gypnode-gyp configure(for debugging usenode-gyp configure -d)node-gyp buildnpm test(for debugging changeindex.jsto load the node module from theDebugfolder and pressF5)