Skip to content

Lib.pressKey

Lib › pressKey

Type: (this: void, params: PressKeyParams) => Promise<void>

Presses a single key (optionally with modifiers) using **trusted** Electron keyboard input, firing the full real key pipeline — keydown → keypress → beforeinput → input → keyup.

This is the key-press analog of Lib.typeIntoEditor: it injects a trusted keyDown → char → keyUp sequence via Electron's webContents.sendInputEvent, so it is delivered to the window's DOM-focused element and flows through the real input pipeline — unlike dispatchEvent(new KeyboardEvent(...)), which is untrusted (isTrusted: false) and ignored by CodeMirror and most key handlers. Use it for special keys ('Enter', 'Escape', 'Tab', arrow keys) and modifier combinations (Shift+Enter, Ctrl+A) that Lib.typeIntoEditor (which types printable text) does not cover.

This is the low-level primitive: it injects the key press and does **not** poll for any effect (a key press has no universal observable outcome — Enter edits the document, Escape closes a modal, ArrowDown moves the selection). The caller focuses the intended target first, then awaits the expected effect via Lib.waitUntil. It targets the single shared window's **global** focus, so only the DOM-focused element receives the key.

It presses the key in the main window unless PressKeyParams.window names a popout. Each Obsidian window is its own web contents, so a key pressed in one never reaches another.

**On mobile** the same sequence is injected through the WebView's debugger (Input.dispatchKeyEvent), which is equally trusted. Named keys ('Enter', 'Escape', 'Tab', 'Backspace', 'Delete', the arrows) and single printable characters are supported; any other multi-character name throws rather than pressing nothing.

**Must be awaited** — see Lib.clickElement.