Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Keyboard Area Widget

The keyboard_area widget wraps a child and captures keyboard events. Use it to build keyboard-driven interactions like shortcuts, navigation, or text handling.

Interface

type KeyEvent = {
  key: string,
  modifiers: { shift: bool, ctrl: bool, alt: bool, logo: bool },
  text: string,
  repeat: bool
};

val keyboard_area: fn(
  ?#on_key_press: fn(KeyEvent) -> Any,
  ?#on_key_release: fn(KeyEvent) -> Any,
  &Widget
) -> Widget

The KeyEvent Type

  • key — the key name (e.g. "a", "Enter", "ArrowUp")
  • modifiers — which modifier keys were held: shift, ctrl, alt, logo (super/command)
  • text — the text produced by the key press (empty for non-character keys)
  • repeattrue if this is an auto-repeat event from holding the key

Parameters

  • on_key_press — called with a KeyEvent when a key is pressed
  • on_key_release — called with a KeyEvent when a key is released

The positional argument is a reference to the child widget.

Examples

use gui;
use gui::text;
use gui::keyboard_area;
use gui::container;
use gui::column;
mod icon;

let last_key = "None";
let mod_name = "None";

[&window(
    #icon: &icon::icon,
    #title: &"Keyboard Area",
    &keyboard_area(
        #on_key_press: |e| {
            last_key <- e ~ e.key;
            mod_name <- e ~ select e.modifiers {
                { ctrl: true, shift: _, alt: _, logo: _ } => "Ctrl",
                { ctrl: _, shift: true, alt: _, logo: _ } => "Shift",
                { ctrl: _, shift: _, alt: true, logo: _ } => "Alt",
                { ctrl: _, shift: _, alt: _, logo: true } => "Logo",
                _ => "None"
            }
        },
        &container(
            #width: &`Fill,
            #height: &`Fill,
            #halign: &`Center,
            #valign: &`Center,
            &column(
                #spacing: &15.0,
                #halign: &`Center,
                &[
                    text(#size: &24.0, &"Keyboard Area Demo"),
                    text(&"Press any key..."),
                    text(#size: &32.0, &last_key),
                    text(&"Modifier: [mod_name]")
                ]
            )
        )
    )
)]

Keyboard Area

See Also