pub struct Key {
pub code: KeyCode,
pub location: KeyLocation,
pub modifiers: KeyModifiers,
pub repeat: bool,
pub text: Option<String>,
pub shifted_key: Option<char>,
pub base_key: Option<char>,
}Expand description
A key event.
Equality and hashing use the logical code, reported
location, and modifiers other than
lock states. They ignore repeat status,
associated text, and alternate layout metadata.
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).
location: KeyLocationThe reported key location.
modifiers: KeyModifiersActive modifiers.
repeat: boolWhether the input protocol identifies this press as an auto-repeat.
false means the input did not identify a repeat. Decoder-generated
releases always use false. Normalization preserves this field.
Win32 records use false for the first occurrence and true for
additional key-down occurrences in the same record.
Equality, hashing, shortcut matching, and display use the key identity
independently of this field.
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, repeat starts false, and
location starts KeyLocation::Standard.
All supplied modifier bits remain intact.
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.
A pattern with an explicit location compares exact key identity.
For example, "kpenter" requires keypad Enter, and "rightctrl" requires
right Ctrl. Check location to require a standard key.
Modifier chords combine with location names: "ctrl+kpenter".
A pattern without an explicit location matches in two steps:
- 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, except that location is ignored. Thus"enter"accepts standard and keypad Enter, and"ctrl"accepts either Ctrl. 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.
These rules apply to printable keypad characters too. Normalization preserves location, repeat metadata, and supplied text and layout values.
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".
Location prefixes attach to the key name, as in "leftctrl", "rightalt",
"kp0", and "kpf1". Modifier chords use "+", as in "ctrl+kp0".
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> ErasedDestructor for Twhere
T: 'static,
§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