Skip to main content

KeyEvent

Enum KeyEvent 

Source
pub enum KeyEvent {
    Down(KeyDownEvent),
    Up(KeyUpEvent),
    Repeat(KeyRepeatEvent),
}
Expand description

Defines the interface for keyboard key events.

The KeyEvent provides a universal model for key event information from a hardware keyboard across platforms.

See also:

  • HardwareKeyboard for full introduction to key event model and handling.
  • KeyDownEvent, the variant for events representing the user pressing a key.
  • KeyRepeatEvent, the variant for events representing the user holding a key, causing repeated events.
  • KeyUpEvent, the variant for events representing the user releasing a key.

Variants§

Implementations§

Source§

impl KeyEvent

Source

pub fn physical_key(&self) -> PhysicalKeyboardKey

Returns an object representing the physical location of this key.

A PhysicalKeyboardKey represents a USB HID code sent from the keyboard, ignoring the key map, modifier keys (like SHIFT), and the label on the key.

PhysicalKeyboardKeys are used to describe and test for keys in a particular location. A PhysicalKeyboardKey may have a name, but the name is a mnemonic (“keyA” is easier to remember than 0x70004), derived from the key’s effect on a QWERTY keyboard. The name does not represent the key’s effect whatsoever (a physical “keyA” can be the Q key on an AZERTY keyboard).

For instance, if you wanted to make a game where the key to the right of the CAPS LOCK key made the player move left, you would be comparing a physical key with PhysicalKeyboardKey::KEY_A, since that is the key next to the CAPS LOCK key on a QWERTY keyboard. This would return the same thing even on an AZERTY keyboard where the key next to the CAPS LOCK produces a “Q” when pressed.

If you want to make your app respond to a key with a particular character on it regardless of location of the key, use logical_key instead.

Also, even though physical keys are defined with USB HID codes, their values are not necessarily the same HID codes produced by the hardware and presented to the driver. On most platforms, Flutter has to map the platform representation back to a HID code because the original HID code is not provided.

Source

pub fn logical_key(&self) -> LogicalKeyboardKey

Returns an object representing the logical key that was pressed.

This method takes into account the key map and modifier keys (like SHIFT) to determine which logical key to return.

If you are looking for the character produced by a key event, use character instead.

Source

pub fn character(&self) -> Option<&str>

Returns the Unicode character (grapheme cluster) completed by this keystroke, if any.

This will only return a character if this keystroke, combined with any preceding keystroke(s), generates a character, and only on a “key down” event. It will return None if no character has been generated by the keystroke (e.g. a “dead” or “combining” key), or if the corresponding key is a key without a visual representation, such as a modifier key or a control key. It will also return None if this is a “key up” event.

This can return multiple Unicode code points, since some characters (more accurately referred to as grapheme clusters) are made up of more than one code point.

The character doesn’t take into account edits by an input method editor (IME), or manage the visibility of the soft keyboard on touch devices.

The character is not available on KeyUpEvents.

Source

pub fn time_stamp(&self) -> Duration

Time of event, relative to an arbitrary start point.

All events share the same time stamp origin.

Source

pub fn device_type(&self) -> KeyEventDeviceType

The source device type for the key event.

Not all platforms supply an accurate type.

Defaults to KeyEventDeviceType::Keyboard.

Source

pub fn synthesized(&self) -> bool

Whether this event is synthesized by Flutter to synchronize key states.

A non-synthesized event is converted from a native event, and a native event can only be converted to one non-synthesized event. Some properties might be changed during the conversion (for example, a native repeat event might be converted to a Flutter down event when necessary.)

A synthesized event is created without a source native event in order to synchronize key states. For example, if the native platform shows that a shift key that was previously held has been released somehow without the key up event dispatched (probably due to loss of focus), a synthesized key up event will be added to regularized the event stream.

For detailed introduction to the regularized event model, see HardwareKeyboard.

A KeyRepeatEvent is never synthesized.

Source

pub fn to_key_data(&self) -> KeyData

The host’s KeyData this event was made from, as KeyEventManager reads it: what an app hands back to the host, such as a key it kept back for the menu bar (MenuBar::perform_key_equivalent).

Trait Implementations§

Source§

impl Clone for KeyEvent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for KeyEvent

Source§

impl From<KeyDownEvent> for KeyEvent

Source§

fn from(event: KeyDownEvent) -> KeyEvent

Converts to this type from the input type.
Source§

impl From<KeyRepeatEvent> for KeyEvent

Source§

fn from(event: KeyRepeatEvent) -> KeyEvent

Converts to this type from the input type.
Source§

impl From<KeyUpEvent> for KeyEvent

Source§

fn from(event: KeyUpEvent) -> KeyEvent

Converts to this type from the input type.
Source§

impl PartialEq for KeyEvent

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for KeyEvent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,