JavaScript Keyboard Shortcuts API

Last modified by Eleni Cojocariu on 2026/10/05 14:04

Reference

Register and remove keyboard shortcuts from JavaScript through the global shortcut object, which the skin loads on every page of the wiki and which drives the bundled Keypress JS library. Put the code in a JavaScript Skin Extension so it runs on the pages you want it on.

Members

MemberParametersReturnsWhat it does
shortcut.add(combination, callback, options)the key combination, the function to run, and an optional map of the options belownothingRegisters a shortcut.
shortcut.remove(combination)the key combinationnothingUnregisters that combination from every listener.
shortcut.all_shortcuts()nonean Array of the registered Keypress combos, each carrying a keys arrayLists every shortcut currently registered on the page.

Options

OptionDefaultWhat it does
targetdocumentListens for the shortcut inside that DOM element only.
disable_in_inputfalseIgnores the shortcut while an input, a textarea, a [contenteditable=true] element or the syntax highlighter has the focus.
typeshortcut.type.SIMPLEPicks how the combination is matched, from the values below.

Any other option is ignored, and logs a deprecation warning to the browser console.

ValueMatches
shortcut.type.SIMPLEThe keys pressed together, once.
shortcut.type.COUNTINGThe keys pressed together, counting the repetitions.
shortcut.type.SEQUENCEThe keys pressed one after the other, as x x x a is.

Key combinations

A combination is lowercased, and its + separators become spaces, before it reaches Keypress. So Ctrl+Alt+N and ctrl alt n register the same shortcut, and the + form is kept only for compatibility with the syntax XWiki used before Keypress.

The library itself is documented at Keypress JS.

Registering a shortcut

shortcut.add("Ctrl+Alt+N", function () {
  new XWiki.widgets.Notification("Congratulations on typing this one!", "done");
}, { disable_in_input: true });

Removing a shortcut

shortcut.remove("Ctrl+Alt+N");

Removing every shortcut

Iterate the Array with for...of: for...in would walk its indexes instead of its combos, and keys would be undefined.

for (const binding of shortcut.all_shortcuts()) {
  shortcut.remove(binding.keys.join(' '));
}

FAQ

Where do I put the code?

In a JavaScript Skin Extension, whose "Use this extension" field decides which pages it loads on.

Can I change the shortcuts XWiki ships with?

Those come from translation keys rather than from this API, and an administrator rebinds them by overriding those keys.

Why does my shortcut fire while I am typing in the editor?

Register it with disable_in_input: true, which stops the listener while an input, a textarea or a content-editable element has the focus.

Related

Get Connected