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: KeyLocationstate: ElementStaterepeat: bool

Implementations

Identical to KeyEvent::text but this is affected by Ctrl.

For example, pressing Ctrl+a produces Some("\x01").

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.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.