Skip to main content

Crate kbd_tao

Crate kbd_tao 

Source
Expand description

Tao key event conversions for kbd.

This crate converts tao’s key events into kbd’s unified types so that window-focused key events (from tao) and global hotkey events (from kbd-global) can feed into the same Dispatcher. This is especially useful in Tauri apps where you want both in-window shortcuts and system-wide hotkeys handled through a single hotkey registry.

tao is Tauri’s fork of winit — both derive from the W3C UI Events specification, so the variant names are nearly identical and the mapping is mechanical. Unlike winit, tao uses KeyCode directly in KeyEvent rather than wrapping it in a PhysicalKey type.

§Extension traits

§Key mapping

taokbdNotes
KeyCode::KeyAKeyCode::KeyZKey::AKey::ZLetters
KeyCode::Digit0KeyCode::Digit9Key::DIGIT0Key::DIGIT9Digits
KeyCode::F1KeyCode::F35Key::F1Key::F35Function keys
KeyCode::Numpad0KeyCode::Numpad9Key::NUMPAD0Key::NUMPAD9Numpad
KeyCode::Enter, KeyCode::Escape, …Key::ENTER, Key::ESCAPE, …Navigation / editing
KeyCode::ControlLeft, …Key::CONTROL_LEFT, …Modifier keys as triggers
KeyCode::SuperLeft / KeyCode::SuperRightKey::META_LEFT / Key::META_RIGHTtao’s Super = kbd’s Meta
KeyCode::Equal / KeyCode::PlusKey::EQUALSame physical key
KeyCode::MediaPlayPause, …Key::MEDIA_PLAY_PAUSE, …Media keys
KeyCode::BrowserBack, …Key::BROWSER_BACK, …Browser keys
KeyCode::Unidentified(_)NoneNo mapping possible

§Modifier mapping

§Usage

Inside tao’s event loop, use TaoEventExt to convert key events directly:

use kbd::prelude::*;
use kbd_tao::TaoEventExt;
use tao::event::{Event, WindowEvent};
use tao::event_loop::{ControlFlow, EventLoop};
use tao::keyboard::ModifiersState;
use tao::window::WindowBuilder;

let event_loop = EventLoop::new();
let _window = WindowBuilder::new().build(&event_loop).unwrap();
let mut modifiers = ModifiersState::empty();

event_loop.run(move |event, _, control_flow| {
    *control_flow = ControlFlow::Wait;
    if let Event::WindowEvent { event, .. } = event {
        match event {
            WindowEvent::ModifiersChanged(mods) => modifiers = mods,
            WindowEvent::KeyboardInput { event, .. } => {
                if let Some(hotkey) = event.to_hotkey(modifiers) {
                    println!("{hotkey}");
                }
            }
            _ => {}
        }
    }
});

The individual conversion traits can also be used separately:

use kbd::prelude::*;
use kbd_tao::{TaoKeyExt, TaoModifiersExt};
use tao::keyboard::{KeyCode, ModifiersState};

let key = KeyCode::KeyA.to_key();
assert_eq!(key, Some(Key::A));

let mods = ModifiersState::CONTROL.to_modifiers();
assert_eq!(mods, vec![Modifier::Ctrl]);

Traits§

TaoEventExt
Convert a tao KeyEvent (plus modifier state) to a kbd Hotkey.
TaoKeyExt
Convert a tao key type to a kbd Key.
TaoModifiersExt
Convert tao ModifiersState bitflags to a sorted Vec<Modifier>.

Functions§

tao_key_to_hotkey
Build a Hotkey from a tao key code and modifier state.