Documentation content

Keyboard Shortcuts

The Native SDK can register app-level keyboard shortcuts and deliver them to both Zig and JavaScript. Shortcuts are matched by key plus modifiers and are scoped to the native window that receives the key event. To react while another app has focus, use global shortcuts.

Manifest

Declare shortcuts in the app manifest. This example uses ZON:

.shortcuts = .{
    .{ .id = "command.palette", .key = "p", .modifiers = .{ "primary", "shift" } },
    .{ .id = "reload", .key = "r", .modifiers = .{ "primary" } },
},

primary maps to Command on macOS and Control on Windows/Linux. Use command, control, option or alt, and shift when a platform-specific modifier is required.

An app can define up to 64 shortcuts. Shortcut ids can be up to 64 bytes and keys can be up to 32 bytes.

Chromium builds are currently macOS-only. Use the system WebView backend on Linux when an app needs native shortcut events.

Runtime

Generated runners register shortcuts from app.json or app.zon automatically. Use RunOptions.shortcuts only when an app needs to override the manifest with a list built in Zig.

Apps that construct a runtime directly can pass shortcuts in Runtime.init:

const shortcuts = [_]native_sdk.Shortcut{
    .{ .id = "command.palette", .key = "p", .modifiers = .{ .primary = true, .shift = true } },
};

var runtime = native_sdk.Runtime.init(.{
    .platform = platform,
    .shortcuts = &shortcuts,
});

Events

Zig apps receive Event.shortcut:

fn event(context: *anyopaque, runtime: *Runtime, ev: Event) anyerror!void {
    _ = context;
    _ = runtime;
    switch (ev) {
        .shortcut => |shortcut| {
            if (std.mem.eql(u8, shortcut.id, "command.palette")) {
                // Open palette
            }
        },
        else => {},
    }
}

The JavaScript bridge emits a shortcut event on window.zero:

window.zero.on("shortcut", (event) => {
  if (event.id === "command.palette") {
    openCommandPalette();
  }
});

The event detail includes id, command, key, windowId, and modifiers. command is an alias for id.

Keys

Portable single-character keys are letters, digits, and the unshifted punctuation keys =, -, ,, ., /, ;, ', [, ], \, and `. Named keys are escape, enter, tab, space, backspace, arrowleft, arrowright, arrowup, arrowdown, delete, home, end, pageup, pagedown, insert, and f1 through f12.

Single-character keys and text-entry keys (enter, tab, space, and backspace) require at least one modifier so shortcuts do not intercept normal typing by default.

Global shortcuts

A global shortcut fires while any app has focus and reports both press and release, for launchers or hold-to-talk. Bindings use the same keys as app shortcuts, up to 16 per app, and every key except f1 through f12 needs a modifier other than Shift. Declare them from a TypeScript core with Sub.globalShortcut(id, binding, arm); the shortcut is registered while the model declares it and re-registered when its binding changes.

export function subscriptions(model: Model): Sub<Msg> {
  return Sub.globalShortcut("talk", { key: asciiBytes("space"), option: true }, "talk");
}

type Msg = { kind: "talk"; id: Uint8Array; state: GlobalShortcutState };

The arm receives the shortcut id and a state:

  • registered: the OS accepted the binding.
  • pressed / released: the keys went down or up. Every press gets a release, even if the binding is removed while held.
  • conflict: the OS or another app owns the combination.
  • unsupported: the platform has no global shortcuts.
  • rejected: the binding is invalid (unknown key, no modifier other than Shift on a key other than f1 through f12, a bad or duplicate id, or more than 16 shortcuts).

Supported on macOS and Windows. Linux reports unsupported. Zig cores use fx.setGlobalShortcuts and fx.clearGlobalShortcuts.

Keyboard input

An app can also send key chords to whichever app has keyboard focus, for example to paste a transcript with primary+V. A chord has the same shape as a global shortcut binding but needs no modifier, so { key: asciiBytes("enter") } presses Return. Declare the keyboard_input permission; on macOS the user must also trust the app in System Settings > Privacy & Security > Accessibility.

Cmd.keyboard.sendChord({ key: asciiBytes("v"), primary: true }, { key: "paste", ok: "pasted", err: "paste_failed" });
Cmd.keyboard.accessStatus({ prompt: true }, { key: "access", ok: "access_checked", err: "access_failed" });

sendChord resolves with empty bytes. accessStatus resolves with granted, denied, or not_required; prompt: true lets macOS show its own dialog. Errors are rejected (no permission), unsupported, invalid_request, denied (no Accessibility grant, nothing posted), blocked (Windows refused the input), and failed. Zig cores use fx.sendKeyChord and fx.keyboardAccessStatus.

macOS and Windows post input (CGEventPost and SendInput). Windows silently drops input aimed at elevated windows, so success means the chord was posted, not that it arrived. Linux, iOS, and Android report unsupported. There is no WebView bridge command, and replay, the fake executor, and the core devhost never post input.

A chord that matches one of the app's own global shortcuts triggers that shortcut instead of reaching the focused app, and on Windows a chord that reuses a held shortcut's key can end its press early. Send chords that differ from the app's global shortcuts.