How it works
- A command has an id (
format.bold,edit.repeat,cursor.wordLeft), a title and a category. - A binding joins a key to a command:
{ key: "mod+b", command: "format.bold" }.modis ⌘ on a Mac and Ctrl elsewhere. - Bindings come in three layers. A later layer wins:
- Word’s defaults (
DOCX_KEYBINDINGS) - your
overrides - the user’s changes, from the Keyboard Shortcuts window
- Word’s defaults (
Change keys in code
keybindings prop.
Key names
Modifiers aremod, meta, ctrl, alt and shift, in any order. Keys are the letter or digit, f1 to f12, enter, tab, space, backspace, delete, escape, up, down, left, right, home, end, pageup, pagedown, contextmenu and punctuation such as [, = or .. The editor reads the physical key, so ⌥ on a Mac still binds as alt+.
Keys that the browser keeps for itself cannot work: Ctrl+W, Ctrl+T, Ctrl+N, Ctrl+Tab, and ⌘Q and ⌘W on a Mac. The browser never sends them to the page, and the Keyboard Shortcuts window refuses them. A binding that does not parse is skipped with a console.warn.
when
A when clause limits a binding to a state of the editor. It reads these names, with !, &&, || and parentheses:
Add your own with
model.keys.setContext("myPanelOpen", true).
If a command declines a key (a table move outside a table), the next binding of that key runs.
Add a command
run returns false to decline the key. register returns a function that removes the command.
Read keys for your UI
The user’s changes
The Keyboard Shortcuts window lists every command. The user opens it with the keyboard button at the end of the toolbar, or from the command palette (⌘⇧P / Ctrl+Shift+P, then Keyboard shortcuts). Withcustomizable: false the button and the window are hidden. They can search by name or key, record new keys (a chord too), see which command already has a key, and reset one command or all.
The changes are kept in localStorage under "docx-editor:keybindings". Only the changes are kept, so a new default in a later release still reaches a user who did not change that command. To keep them somewhere else, pass a store:
store: nullkeeps changes for the page only.memoryKeybindings()does the same and lets you read them.customizable: falsehides the window.model.keys.exportJson()andimportJson(text)move the changes as a file.