Menu

Guides

Keyboard shortcuts and Chromium commands

Decide which keys your app takes, which the page gets and which of Chromium's commands run.

A Kurogane window runs only Chromium’s page-local commands. Those are reload, find, print, zoom, editing, fullscreen, closing the window and DevTools. Kurogane refuses the rest, such as new windows, tabs, history and bookmarks. Two hooks narrow that further.

The shortcuts

Kurogane’s shortcuts are the same on every OS. macOS takes Cmd for Ctrl.

Command Keys macOS Order
Close the window Ctrl+W, Ctrl+Shift+W Cmd+W, Cmd+Shift+W before the page
Quit Cmd+Q before the page
Reload Ctrl+R, F5 Cmd+R page first
Find Ctrl+F, Ctrl+G, F3 Cmd+F, Cmd+G page first
Print Ctrl+P Cmd+P page first
Zoom Ctrl+=, Ctrl+-, Ctrl+0 Cmd+=, Cmd+-, Cmd+0 page first
Back, forward Alt+Left, Alt+Right Cmd+[, Cmd+] page first
Fullscreen F11 Cmd+Ctrl+F page first
DevTools Ctrl+Shift+I, F12 Cmd+Option+I page first

Closing and quitting run before the page sees the key. No page keeps them. Every other shortcut reaches the page first. It runs only when the page doesn’t call preventDefault().

Chromium’s keys for what Kurogane refuses (Ctrl+T, Ctrl+N, Ctrl+Shift+T and Ctrl+Tab) are ordinary keys the page gets.

Add a shortcut of your own

App::on_key sees each key press before a shortcut and the page do. KeyDecision::Consume takes the key for your app:

use kurogane::{App, Key, KeyDecision};

fn main() {
    App::new("content")
        .on_key(|key, app| {
            // Ctrl+K (Cmd+K on macOS) opens the page's command palette
            if key.modifiers().primary() && key.key() == Key::Char('K') {
                app.broadcast_json("palette", &true);
                KeyDecision::Consume
            } else {
                KeyDecision::Default
            }
        })
        .run_or_exit();
}

The page listens for the event with kurogane.on("palette", ...). A consumed key goes nowhere else. No shortcut runs and the page sees none of the key, its character or its release.

KeyPress describes the press:

  • key() names a key by its place. Shift+W is still Key::Char('W').
  • modifiers().primary() is Ctrl on Windows and Linux and Cmd on macOS.
  • command() names the command the key runs as one of Kurogane’s shortcuts on any OS.
  • in_editable_field() says whether the user is typing into a field. Check it before taking keys the user may be typing.
  • repeat() says whether the key is held down and repeating.
  • browser() names the browser the key went to.

The hook sees presses and their repeats. It never sees releases.

Turn off a shortcut

Consume the key of the command you want gone:

use kurogane::{App, ChromeCommand, KeyDecision};

fn main() {
    App::new("content")
        .on_key(|key, _app| {
            // Ctrl+W (Cmd+W on macOS) doesn't close this window
            if key.command() == Some(ChromeCommand::Close) {
                KeyDecision::Consume
            } else {
                KeyDecision::Default
            }
        })
        .run_or_exit();
}

The window’s close button and closing it from your app still work.

Let the page handle Ctrl+W

KeyDecision::PageFirst gives the page closing and quitting first too. The shortcut runs only when the page doesn’t call preventDefault():

use kurogane::{App, ChromeCommand, KeyDecision};

fn main() {
    App::new("content")
        .on_key(|key, _app| {
            if key.command() == Some(ChromeCommand::Close) {
                KeyDecision::PageFirst
            } else {
                KeyDecision::Default
            }
        })
        .run_or_exit();
}

PageFirst is for shortcuts:

  • Chromium drops the character of a shortcut the page left alone. A typing key answered PageFirst types nothing unless the page handles its keydown. Answer it for chords such as Ctrl+W and not for every key. AltGr arrives as Ctrl+Alt on Windows.
  • It hands the shortcut to the page. A page that prevents Ctrl+W keeps its window open. A page that hangs holds the key.
  • Answer it only for windows that show your own pages. key.browser() says which window the key went to.
  • A browser embedded in your own window runs no shortcuts. There PageFirst changes nothing but the dropped character.

Refuse a command

App::on_chrome_command is asked about each command a window would run from a shortcut or the context menu. CommandDecision::Refuse stops it:

use kurogane::{App, ChromeCommand, CommandDecision};

fn main() {
    App::new("content")
        .on_chrome_command(|request, _app| match request.command() {
            // No DevTools and no reload in release builds
            ChromeCommand::DevTools | ChromeCommand::Reload if !cfg!(debug_assertions) => {
                CommandDecision::Refuse
            }
            _ => CommandDecision::Default,
        })
        .run_or_exit();
}

ChromeCommand groups Chromium’s commands by what the user asked for. Refusing DevTools refuses Ctrl+Shift+I, F12 and the context menu’s Inspect alike. Refusing Reload refuses every kind of reload.

The hook can only refuse. It’s never asked about a command Kurogane refuses and can’t allow one. A key on_key consumed never becomes a command. Neither does one the page prevented after PageFirst.

Rules for the hooks

  • Both hooks run on the UI thread. on_key runs for every key press. Keep them quick.
  • A panicking on_key lets the key through. A panicking on_chrome_command refuses the command.
  • DevTools’ own windows reach neither hook.
  • A browser embedded in your own window reaches on_key. Chromium runs no commands there and on_chrome_command is never asked.
  • A later call replaces an earlier hook.