Skip to main content

Module keys

Module keys 

Source
Expand description

Keyboard shortcuts that apps can rebind.

Every interactive component has an Action enum and a default_keymap(), and an app can bind, unbind or clear the defaults. Some components, such as tabs and sliders, resolve their keymap themselves while they have keyboard focus or were the last thing pressed. For shortcuts that work from anywhere, the app subscribes with subscription, stores each Event it receives, and resolves it through a component’s Keymap into that component’s own action. Here the app switches tabs with Ctrl+Tab from anywhere, while the tab list keeps its arrow keys for when it has focus.

use iced::Subscription;
use iced::keyboard::key::Named;
use iced_cube::keys::{self, Chord, Keymap};
use iced_cube::navigation::tabs::{self, Action};

#[derive(Debug, Clone)]
enum Message {
    Key(keys::Event),
    Tabs(tabs::Event<u8>),
}

struct App {
    tabs: tabs::State<u8>,
    keymap: Keymap<Action>,
}

impl App {
    fn new() -> Self {
        let keymap = Keymap::new()
            .bind(Chord::named(Named::Tab).ctrl(), Action::Next)
            .bind(Chord::named(Named::Tab).ctrl().shift(), Action::Previous);
        Self { tabs: tabs::State::new([tabs::tab(1, "One"), tabs::tab(2, "Two")]), keymap }
    }

    fn update(&mut self, message: Message) {
        let event = match message {
            Message::Key(key) => self
                .keymap
                .resolve_event(&key)
                .and_then(|action| action.event(&self.tabs)),
            Message::Tabs(event) => Some(event),
        };
        if let Some(event) = event {
            let _ = self.tabs.update(event);
        }
    }

    fn subscription(&self) -> Subscription<Message> {
        keys::subscription().map(Message::Key)
    }
}

Structs§

Chord
A key plus the modifiers held with it, such as Ctrl+Shift+Tab.
Event
A key press delivered by subscription.
Keymap
Chords bound to actions. Each chord triggers at most one action; an action may have several chords.
Tracker
Turns raw iced events into the Events that subscription delivers. Apps that listen to window events themselves can feed it every event, captured or not, to get the same behaviour.

Enums§

ParseChordError
Why a string is not a valid Chord.

Traits§

Action
A component action that can be bound to keys.

Functions§

subscription
Key presses that no widget captured, such as a focused text input, plus lone modifier keys as described on Event. With the icedwright-remote feature it also hears the keys an icedwright remote session sends.