Skip to main content

WinitEventExt

Trait WinitEventExt 

Source
pub trait WinitEventExt: Sealed {
    // Required method
    fn to_hotkey(&self, modifiers: ModifiersState) -> Option<Hotkey>;
}
Expand description

Convert a winit KeyEvent (plus modifier state) to a kbd Hotkey.

Winit’s KeyEvent does not include modifier state — modifiers are tracked via WindowEvent::ModifiersChanged. Pass the current ModifiersState alongside the event.

Returns None if the physical key has no kbd equivalent.

When the key is itself a modifier (e.g., ControlLeft), the corresponding modifier flag is stripped from the modifiers — winit includes the pressed modifier key in its own state, but kbd treats the key as the trigger, not as a modifier of itself.

This trait is sealed and cannot be implemented outside this crate.

Required Methods§

Source

fn to_hotkey(&self, modifiers: ModifiersState) -> Option<Hotkey>

Convert this key event to a Hotkey, or None if the key is unmappable.

Pass the current ModifiersState from WindowEvent::ModifiersChanged.

§Examples

Winit’s KeyEvent cannot be easily constructed in doctests, so this trait is used inside winit’s event loop where the framework provides the event. For standalone conversion use winit_key_to_hotkey:

use kbd::prelude::*;
use kbd_winit::winit_key_to_hotkey;
use winit::keyboard::{KeyCode, ModifiersState, PhysicalKey};

let hotkey = winit_key_to_hotkey(
    PhysicalKey::Code(KeyCode::KeyS),
    ModifiersState::CONTROL,
);
assert_eq!(
    hotkey,
    Some(Hotkey::new(Key::S).modifier(Modifier::Ctrl)),
);

Implementations on Foreign Types§

Source§

impl WinitEventExt for KeyEvent

Source§

fn to_hotkey(&self, modifiers: ModifiersState) -> Option<Hotkey>

Implementors§