Skip to content

Lib.pressKey method

Lib › pressKey

Presses a single key (optionally with modifiers) using **trusted** Electron keyboard input, firing the full real key pipeline — keydownkeypressbeforeinputinputkeyup.

This is the key-press analog of Lib.typeIntoEditor: it injects a trusted keyDowncharkeyUp 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.

Synchronous: injecting the trusted key press does no real awaiting, so the caller does not need to await it.

Signature:

pressKey(this: void, params: PressKeyParams): void

Parameters:

ParameterTypeDescription
thisvoid
paramsPressKeyParamsThe key to press and any modifiers to hold.

Returns: void