pub struct KeyEvent {
pub physical_key: KeyCode,
pub logical_key: Key<'static>,
pub text: Option<&'static str>,
pub location: KeyLocation,
pub state: ElementState,
pub repeat: bool,
/* private fields */
}
Expand description
Describes a keyboard input targeting a window.
Fields§
§physical_key: KeyCode
Represents the position of a key independent of the currently active layout.
It also uniquely identifies the physical key (i.e. it’s mostly synonymous with a scancode).
The most prevalent use case for this is games. For example the default keys for the player
to move around might be the W, A, S, and D keys on a US layout. The position of these keys
is more important than their label, so they should map to Z, Q, S, and D on an “AZERTY”
layout. (This value is KeyCode::KeyW
for the Z key on an AZERTY layout.)
Note that Fn
and FnLock
key events are not guaranteed to be emitted by tao
. These
keys are usually handled at the hardware or OS level.
logical_key: Key<'static>
This value is affected by all modifiers except Ctrl.
This has two use cases:
- Allows querying whether the current input is a Dead key.
- Allows handling key-bindings on platforms which don’t
support
key_without_modifiers
.
§Platform-specific
- Web: Dead keys might be reported as the real key instead
of
Dead
depending on the browser/OS.
text: Option<&'static str>
Contains the text produced by this keypress.
In most cases this is identical to the content
of the Character
variant of logical_key
.
However, on Windows when a dead key was pressed earlier
but cannot be combined with the character from this
keypress, the produced text will consist of two characters:
the dead-key-character followed by the character resulting
from this keypress.
An additional difference from logical_key
is that
this field stores the text representation of any key
that has such a representation. For example when
logical_key
is Key::Enter
, this field is Some("\r")
.
This is None
if the current keypress cannot
be interpreted as text.
See also: text_with_all_modifiers()
location: KeyLocation
§state: ElementState
§repeat: bool
Implementations§
Source§impl KeyEvent
impl KeyEvent
Sourcepub fn text_with_all_modifiers(&self) -> Option<&str>
pub fn text_with_all_modifiers(&self) -> Option<&str>
Identical to KeyEvent::text
but this is affected by Ctrl.
For example, pressing Ctrl+a produces Some("\x01")
.
Sourcepub fn key_without_modifiers(&self) -> Key<'static>
pub fn key_without_modifiers(&self) -> Key<'static>
This value ignores all modifiers including, but not limited to Shift, Caps Lock, and Ctrl. In most cases this means that the unicode character in the resulting string is lowercase.
This is useful for key-bindings / shortcut key combinations.
In case logical_key
reports Dead
, this will still report the
key as Character
according to the current keyboard layout. This value
cannot be Dead
.