CodeMirror's key binding system provides flexible keyboard interaction support with platform-specific modifiers, multi-stroke keys, and contextual commands.
interface KeyBinding {
key: string;
run: Command;
shift?: Command;
scope?: string;
preventDefault?: boolean;
}const keymap: Facet<readonly KeyBinding[]>;
function runScopeHandlers(view: EditorView, event: KeyboardEvent, scope: string): boolean;import { keymap } from "@codemirror/view";
const basicKeys = keymap.of([
{ key: "Ctrl-s", run: saveCommand },
{ key: "Ctrl-z", run: undoCommand },
{ key: "Ctrl-y", run: redoCommand },
{ key: "Mod-a", run: selectAllCommand } // Ctrl on PC, Cmd on Mac
]);const multiStrokeKeys = keymap.of([
{ key: "Ctrl-k Ctrl-c", run: commentCommand },
{ key: "Ctrl-k Ctrl-u", run: uncommentCommand }
]);const conditionalKeys = keymap.of([
{
key: "Tab",
run: (view) => {
if (view.state.selection.ranges.some(r => !r.empty)) {
return indentSelection(view);
}
return insertTab(view);
}
}
]);