pub struct Key {
pub code: KeyCode,
pub modifiers: KeyModifiers,
pub text: Option<String>,
pub shifted_key: Option<char>,
pub base_key: Option<char>,
}Expand description
A key event.
Equality and hashing intentionally consider only code
and the binding portion of modifiers: two
keys representing the same press compare equal regardless of
whether informational fields (text,
shifted_key, base_key)
were populated by the producing decoder, and regardless of lock
state (CAPS_LOCK,
NUM_LOCK,
SCROLL_LOCK). This makes Key safe
to use as a HashMap key for binding lookups across decoder paths
and across keyboard latch states.
For string-based binding matching that additionally folds letter
case and consults text for layout-specific glyphs,
see matches and matches_any.
Fields§
§code: KeyCodeThe key code (which key was pressed).
modifiers: KeyModifiersActive modifiers.
text: Option<String>Associated text (from Kitty protocol or composed input).
Informational; ignored by == and Hash. Consulted by
Key::matches when it carries a layout-specific glyph that
differs from code (for example "!" for shift+1 on a US
layout), enabling layout-independent string bindings.
shifted_key: Option<char>Shifted-layer codepoint (Kitty Report-Alternate-Keys).
Informational; ignored by == and Hash.
base_key: Option<char>Base-layout codepoint (Kitty Report-Alternate-Keys).
Informational; ignored by == and Hash.
Implementations§
Source§impl Key
impl Key
Sourcepub fn new(code: KeyCode, modifiers: KeyModifiers) -> Self
pub fn new(code: KeyCode, modifiers: KeyModifiers) -> Self
Construct a Key with the given code and modifiers.
This is a transparent constructor: no canonicalization is
performed. Optional fields (text, shifted_key, base_key)
start empty. Decoder paths populate the optional fields as
needed and then chain Key::normalized (or call
Key::normalize in place) to apply the canonical identity
rules (case folding, printable text auto-population).
Sourcepub fn normalized(self) -> Self
pub fn normalized(self) -> Self
Consume self and return the canonical form. The recommended
way to build a canonical key in expression position
(Key::new(code, mods).normalized()); see Key::normalize
for the in-place equivalent and the full list of rules.
Sourcepub fn char(&self) -> Option<char>
pub fn char(&self) -> Option<char>
Return the character if this is a simple character key.
KeyCode::Space reports ' '; other named keys report None.
Sourcepub fn matches(&self, pattern: &str) -> bool
pub fn matches(&self, pattern: &str) -> bool
Test whether this key matches a string pattern.
Matching is two-tier:
- If this key carries a
textvalue and it equalspatternbyte-for-byte, the match succeeds. Lets bindings be written as the produced glyph ("?","!","@") and hit any keystroke that resolves to that text, independent of keyboard layout. Note thattextreflects the user-perceived character, soCapsLockcan shift the matched form (pressinggwith CapsLock on yieldstext == "G"and matches the pattern"G"). - Otherwise,
patternis parsed as aKeywith the same grammar asFromStr(for example"ctrl+c","shift+f1","alt+plus") and compared withPartialEq. Lock state (CapsLock,NumLock,ScrollLock) is ignored by==, so step 2 is layout- sensitive but lock-insensitive. Matching is otherwise case-sensitive —"g"and"G"are distinct ("G"is a synonym for"shift+g").
Returns false for any pattern that fails to parse and has
no matching text; invalid patterns never panic.
§Examples
use uncurses::event::{Key, KeyCode, KeyModifiers};
// Case-sensitive: g and G are distinct (vim-style).
let plain_g = Key::new(KeyCode::Char('g'), KeyModifiers::empty()).normalized();
let big_g = Key::new(KeyCode::Char('G'), KeyModifiers::empty()).normalized();
assert!(plain_g.matches("g"));
assert!(!plain_g.matches("G"));
assert!(big_g.matches("G"));
assert!(big_g.matches("shift+g")); // "G" and "shift+g" are synonyms.
assert!(!big_g.matches("g"));Sourcepub fn matches_any<I, S>(&self, patterns: I) -> bool
pub fn matches_any<I, S>(&self, patterns: I) -> bool
Test whether this key matches any pattern in the iterator.
Equivalent to calling matches for each
pattern and stopping at the first hit. Accepts any iterable
yielding string-like items, including arrays, slices, and
vectors.
§Examples
use uncurses::event::{Key, KeyCode, KeyModifiers};
let key = Key::new(KeyCode::Char('c'), KeyModifiers::CTRL).normalized();
assert!(key.matches_any(["esc", "ctrl+c", "q"]));
assert!(!key.matches_any(["esc", "q"]));Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Apply the canonical identity rules in place. Idempotent.
Decoder paths construct keys via Key::new, populate optional
fields, and then call this method (or chain
Key::normalized) to produce the canonical form. The rules
applied are:
- Case folding for printable
Charcodes: uppercase chars are lowercased and the original is stored inshifted_key;SHIFTis added only whenCAPS_LOCKis not set (with CapsLock the case change is attributed to the lock state, not a Shift press). - Printable text auto-population when
textis empty and the modifier set is a subset ofSHIFT | CAPS_LOCK | NUM_LOCK.
Trait Implementations§
Source§impl Display for Key
Formats the key as its canonical binding spelling: a modifier prefix
(ctrl+, alt+, shift+, super+, hyper+, meta+) followed by the
KeyCode name, for example "ctrl+c", "shift+f1", or "space".
impl Display for Key
Formats the key as its canonical binding spelling: a modifier prefix
(ctrl+, alt+, shift+, super+, hyper+, meta+) followed by the
KeyCode name, for example "ctrl+c", "shift+f1", or "space".
This is the inverse of FromStr: every string it
produces parses back to an equal Key. It renders the structural key
identity only and intentionally ignores the informational
text, shifted_key, and
base_key fields, along with lock state
(CapsLock/NumLock/ScrollLock). It therefore always spells the space
bar as "space" (never " "), and composed or IME input is shown by its
code, not its produced glyph.
For the user-perceived character that was typed, use
char or read text directly; for
layout-aware string matching that consults text, use
matches.
impl Eq for Key
Auto Trait Implementations§
impl Freeze for Key
impl RefUnwindSafe for Key
impl Send for Key
impl Sync for Key
impl Unpin for Key
impl UnsafeUnpin for Key
impl UnwindSafe for Key
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString]. Read more