Skip to content
Sheetwrite

Grid

Keyboard shortcuts panel

Let users change the grid's keys in a Keyboard shortcuts panel, and store their choice.

Edit on GitHubReport a problem

On this page

The Keyboard shortcuts panel lets the user change the keys of the grid's built-in commands. It opens from File › Options in the ribbon, from Shortcuts in the compact ribbon's tab row, or from your own menu. Excel's keys stay the defaults: a user who changes nothing gets exactly the keys on Selection and keyboard.

How a user changes a command's keysFile › Options › Keyboard Shortcuts, or the gear of the compact ribbon, opens the panel. The user clicks Bold's keys and presses Ctrl+I; Escape would stop recording instead. Ctrl+I belongs to Italic, so the panel offers Swap, Replace or Cancel, and each is one change that applies at once. The grid then emits key-bindings-change, and the host stores the bindings and passes them back as config.keyboard.bindings.File › OptionsKeyboard Shortcuts, orthe compact ribbon's gearPress the new keysCtrl+I for Bold;Escape stops recordingCtrl+I is Italic'sSwap, Replace or Cancel;each is one changekey-bindings-changeThe host stores them and passesconfig.keyboard.bindings back

Shortcuts are a user preference, not part of the workbook. The grid applies each change at once and emits key-bindings-change; you store the bindings and pass them back through config.keyboard.bindings.

Turn the panel on and keep the user's keys
const saved = localStorage.getItem("sheetwrite-keys");
keyboard: { bindings: saved ? JSON.parse(saved) : {} },
});
grid.on("key-bindings-change", ({ bindings }) => {
localStorage.setItem("sheetwrite-keys", JSON.stringify(bindings));
});
Full setup
Grid with the Keyboard shortcuts panel
import { createGrid, initSheetwrite, type Workbook } from "@sheetwrite/core";
import "@sheetwrite/core/styles.css";
const host = document.querySelector<HTMLElement>("#grid");
if (host === null) throw new Error("Missing #grid host");
host.style.height = "480px";
const workbook: Workbook = {
activeSheet: "sheet1",
sheets: [{ id: "sheet1", name: "Sheet1", rowCount: 100, columns: [{ key: "a", header: "A", type: "text", width: 120 }] }],
};
const grid = createGrid(host, { workbook, config: { ribbon: true } });
const saved = localStorage.getItem("sheetwrite-keys");
ribbon: true,
keyboard: { bindings: saved ? JSON.parse(saved) : {} },
});
const stop = grid.on("key-bindings-change", ({ bindings }) => {
localStorage.setItem("sheetwrite-keys", JSON.stringify(bindings));
});
// Tear down when the page goes away.
window.addEventListener("pagehide", () => {
stop();
});

bindings holds only the commands whose keys differ from Excel's defaults, so it is {} after Reset all. Its shape is the one config.keyboard.bindings takes, and the grid checks it for conflicts as it does any host binding.

What the user can do

In the panelWhat happens
Type in the search boxThe list shows the commands whose name, group or keys match: fill, or ctrl+shift+l.
Press keys in the search boxThe list shows the command those keys run. Ctrl+A, C, V, X, Z and Y and the arrows still edit the search text.
Show changed onlyThe list shows only the commands with keys other than Excel's.
Click a key, or +Recording starts and is announced. The next keys pressed become the command's keys. Escape stops recording.
× beside a keyThe command loses those keys.
ResetThe command gets Excel's keys back. Reset all does it for every command.
UndoRestores the keys from before the last change.
Export / Import…Saves the bindings as a JSON file, or loads one. An import is one change.

Each column has a job:

  • Command: the name the ribbon uses, grouped as in the ribbon.
  • Keys: what the command runs on now, shown as Ctrl, Shift and Alt, or as ⌘ ⇧ ⌥ ⌃ on a Mac.
  • Excel default: the keys Excel uses, so the user always sees what they moved away from.

Keys are saved as Mod+… when the user holds Ctrl, or ⌘ on a Mac, so one binding works on both.

Proposals beyond Excel

Excel has no general shortcut editor. Each of these sits on top of Excel's keys and changes nothing for a user who doesn't use it:

  • Press keys to search: answers "what does Ctrl+Shift+L do?" with one key press, without knowing the command's name.
  • Excel's default beside each binding: the user can always see what they changed, and Reset is one click away.
  • Undo after every change: a wrong key press costs nothing, so trying keys out is safe.
  • Show changed only: reviewing a setup, or sharing it, means reading a short list instead of every command.
  • Export and import: the user's keys move between machines, and a team can share one setup.

When the keys are taken

If the new keys already belong to another command, the panel names it, for example "Ctrl+I is assigned to Italic.", and offers three choices:

  • Swap: the other command takes the keys you replaced. This is offered when you changed a key, not when you added one.
  • Replace: the other command loses the keys.
  • Cancel: nothing changes.

Each choice is one change and one key-bindings-change event.

The panel refuses keys it can't use, and says why:

  • Keys without Ctrl, ⌘ or Alt type into the cell. Only function keys, Delete and Insert can stand alone.
  • Keys that move or edit in the sheet, such as arrows, Enter, Tab, Space, Page Up and F2, keep their Excel meaning.
  • F6 and Shift + F6 move the focus to the next or previous part of the window and out of the grid, as in Excel, so they stay free. The panel's hint says so.
  • Keys the browser or the operating system keeps never reach the page.

The keys the browser or the operating system keeps:

WhereKeysSource
Chrome and Edge, Windows and LinuxCtrl+N, Ctrl+Shift+N, Ctrl+T, Ctrl+Shift+T, Ctrl+W, Ctrl+Shift+W, Ctrl+Tab, Ctrl+Shift+Tab, Ctrl+PageUp, Ctrl+PageDown, Ctrl+F4Chromium's reserved commands
Chrome and Edge, macOS⌘N, ⌘⇧N, ⌘T, ⌘⇧T, ⌘W, ⌘⇧W, ⌃Tab, ⌃⇧Tab, ⌃PageUp, ⌃PageDown, ⌘⇧[, ⌘⇧], ⌘⌥←, ⌘⌥→the same list, with Chromium's Mac bindings
FirefoxCtrl or ⌘ with N, T, W, Shift+W and Shift+P; Ctrl+Q on Linux; Ctrl+Shift+Q on WindowsFirefox's reserved keys
macOS, every browser⌘Q, ⌘H, ⌘M, ⌘Tab, ⌘`, ⌘Space, ⌘⇧3, ⌘⇧4, ⌘⇧5, ⌘⌥Esc, ⌃ with an arrowApple's Mac keyboard shortcuts
Windows, every browserthe Windows key with anything, Alt+Tab, Alt+F4, Ctrl+Alt+Delete, Ctrl+Shift+EscMicrosoft's Keyboard shortcuts in Windows
Linux, every browserthe Super key with anything, Alt+Tab, Alt+F2, Alt+F4, Ctrl+Alt+DeleteGNOME's Useful keyboard shortcuts

Chromium passes its reserved keys to the page in full screen and in installed-app windows, but the panel still refuses them, because they don't work in an ordinary tab. Safari publishes no list of reserved keys, so in Safari the panel refuses only the macOS keys.

Excel's own defaults stay, even where they never arrive. On a Mac, ⌘⇧3 takes a screenshot, so the Date row says so and the user can choose other keys.

Lock commands

Pass locked to show some commands read-only. A swap or a replace never changes a locked command either; the panel then offers only Cancel.

Keep Copy and Paste on their keys
grid.setConfig({ keyboardSettings: { locked: ["copy", "paste"] } });

If config.keyboard is false or a function, your app handles the keys itself, and the panel shows every command read-only.

Open it from your own menu

grid.openKeyboardSettings() opens the panel when keyboardSettings is set, and does nothing otherwise. The panel's code loads the first time it opens, so a grid that never opens it doesn't download it. Keys pressed while it loads are not lost.

A Settings button in your app
document.querySelector("#keyboard-settings")?.addEventListener("click", () => {
});

Framework adapters

The adapters pass the event on as onKeyBindingsChange (React and Svelte) and @key-bindings-change (Vue):

Vue
<SheetwriteGrid
:workbook="workbook"
:config="{ keyboardSettings: true, keyboard: { bindings } }"
@key-bindings-change="(event) => bindings = event.bindings"
/>
Svelte
config={{ keyboardSettings: true, keyboard: { bindings } }}
onKeyBindingsChange={(event: GridEvents["key-bindings-change"]) => bindings = event.bindings}
/>

Accessibility

  • The dialog: a modal labeled "Keyboard shortcuts". Tab and Shift+Tab stay inside it, and the search box has focus when it opens.
  • Recording: the key being recorded is a button with focus. The status line announces "Recording keys for Bold. Press the new keys, or Escape to cancel."
    • Escape stops recording, and a second Escape closes the dialog.
    • Tab stops recording and moves on, so the keyboard is never trapped.
  • Conflicts: the conflict choices are a labeled group, and the first choice takes focus.
  • Status line: results, refusals and undo are announced there.
  • Closing: focus returns to the element that opened the panel.