kbd-winit
kbd bridge for winit — converts key events and modifiers to kbd types.
This lets window-focused key events (from winit) and global hotkey events (from kbd-global) feed into the same Dispatcher. Useful in applications where you want both in-window shortcuts and system-wide hotkeys handled through a single hotkey registry.
Both winit and kbd derive from the W3C UI Events specification, so the variant names are nearly identical and the mapping is mechanical. Winit wraps key codes in a PhysicalKey type and tracks modifiers separately via WindowEvent::ModifiersChanged, so WinitEventExt takes ModifiersState as a parameter.
[]
= "0.1"
= "0.1"
Extension traits
WinitKeyExt— converts a winitPhysicalKeyorKeyCodeto akbd::KeyWinitModifiersExt— converts winitModifiersStateto aVec<Modifier>WinitEventExt— converts a winitKeyEventplusModifiersStateto akbd::Hotkey
Usage
Inside winit's event loop, use WinitEventExt to convert key events directly:
use *;
use WinitEventExt;
use ApplicationHandler;
use WindowEvent;
use ;
use ModifiersState;
use ;
let event_loop = new.unwrap;
let mut app = App ;
event_loop.run_app.unwrap;
The individual conversion traits can also be used separately:
use *;
use ;
use ;
let key = KeyA.to_key;
assert_eq!;
let key = Code.to_key;
assert_eq!;
let mods = CONTROL.to_modifiers;
assert_eq!;
Key mapping
| winit | kbd | Notes |
|---|---|---|
KeyCode::KeyA – KeyCode::KeyZ |
Key::A – Key::Z |
Letters |
KeyCode::Digit0 – KeyCode::Digit9 |
Key::DIGIT0 – Key::DIGIT9 |
Digits |
KeyCode::F1 – KeyCode::F35 |
Key::F1 – Key::F35 |
Function keys |
KeyCode::Numpad0 – KeyCode::Numpad9 |
Key::NUMPAD0 – Key::NUMPAD9 |
Numpad |
KeyCode::Enter, KeyCode::Escape, … |
Key::ENTER, Key::ESCAPE, … |
Navigation / editing |
KeyCode::ControlLeft, … |
Key::CONTROL_LEFT, … |
Modifier keys as triggers |
KeyCode::SuperLeft / KeyCode::Meta |
Key::META_LEFT |
winit's Super = kbd's Meta |
KeyCode::SuperRight |
Key::META_RIGHT |
|
KeyCode::MediaPlayPause, … |
Key::MEDIA_PLAY_PAUSE, … |
Media keys |
KeyCode::BrowserBack, … |
Key::BROWSER_BACK, … |
Browser keys |
PhysicalKey::Unidentified(_) |
None |
No mapping possible |
Modifier mapping
| winit | kbd |
|---|---|
CONTROL |
Modifier::Ctrl |
SHIFT |
Modifier::Shift |
ALT |
Modifier::Alt |
SUPER |
Modifier::Super |
License
kbd-winit is licensed under the MIT license. See the LICENSE file for more information.