Skip to main content

InputState

Struct InputState 

Source
pub struct InputState {
    pub keys_pressed: HashSet<String>,
    pub keys_held: HashSet<String>,
    pub keys_released: HashSet<String>,
    pub mouse_buttons_pressed: HashSet<MouseButton>,
    pub mouse_buttons_held: HashSet<MouseButton>,
    pub mouse_buttons_released: HashSet<MouseButton>,
    pub mouse_position: Vector2D,
    pub mouse_delta: Vector2D,
    pub mouse_moved: bool,
    pub touch_points: HashMap<i32, Vector2D>,
    pub touch_started: HashSet<i32>,
    pub touch_ended: HashSet<i32>,
}
Expand description

Tracks the current state of all input devices (keyboard, mouse, touch) for a single game frame. The state should be updated by event handlers and cleared of per-frame data at the end of each frame.

Fields§

§keys_pressed: HashSet<String>

Key codes that were pressed during this frame.

get_keys_pressed is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§keys_held: HashSet<String>

Key codes that are currently held down.

get_keys_held is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§keys_released: HashSet<String>

Key codes that were released during this frame.

get_keys_released is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§mouse_buttons_pressed: HashSet<MouseButton>

Mouse buttons that were pressed during this frame.

get_mouse_buttons_pressed is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§mouse_buttons_held: HashSet<MouseButton>

Mouse buttons that are currently held down.

get_mouse_buttons_held is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§mouse_buttons_released: HashSet<MouseButton>

Mouse buttons that were released during this frame.

get_mouse_buttons_released is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§mouse_position: Vector2D

The current mouse position in screen coordinates.

get_mouse_position is public because tests/input/fn.rs, tests/input_state/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§mouse_delta: Vector2D

The mouse position delta (movement) since the last frame.

get_mouse_delta is public because tests/input/fn.rs, tests/input_state/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§mouse_moved: bool

Whether the mouse has moved during this frame.

get_mouse_moved is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§touch_points: HashMap<i32, Vector2D>

Active touch points mapped by identifier to screen position.

get_touch_points is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§touch_started: HashSet<i32>

Touch point identifiers that started during this frame.

get_touch_started is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

§touch_ended: HashSet<i32>

Touch point identifiers that ended during this frame.

get_touch_ended is public because tests/input/fn.rs reads this field from outside this crate. get_mut / set stay crate-internal.

Implementations§

Source§

impl InputState

Implements input state management for InputState.

Source

pub fn press_key(&mut self, key_code: String)

Records a key press event, adding to keys_pressed and keys_held.

§Arguments
  • String - The key code string (e.g., "KeyA", "Space").
Source

pub fn release_key(&mut self, key_code: String)

Records a key release event, moving from keys_held to keys_released.

§Arguments
  • String - The key code string.
Source

pub fn is_key_pressed<K>(&self, key_code: K) -> bool
where K: AsRef<str>,

Tests whether a key was pressed during this frame.

§Arguments
  • K - The key code string.
§Returns
  • bool - True if the key was pressed this frame.
Source

pub fn is_key_held<K>(&self, key_code: K) -> bool
where K: AsRef<str>,

Tests whether a key is currently held down.

§Arguments
  • K - The key code string.
§Returns
  • bool - True if the key is held.
Source

pub fn is_key_released<K>(&self, key_code: K) -> bool
where K: AsRef<str>,

Tests whether a key was released during this frame.

§Arguments
  • K - The key code string.
§Returns
  • bool - True if the key was released this frame.
Source

pub fn press_mouse_button(&mut self, button: MouseButton, position: Vector2D)

Records a mouse button press at the given position.

§Arguments
  • MouseButton - The button that was pressed.
  • Vector2D - The mouse position.
Source

pub fn release_mouse_button(&mut self, button: MouseButton)

Records a mouse button release.

§Arguments
  • MouseButton - The button that was released.
Source

pub fn update_mouse_position(&mut self, position: Vector2D)

Updates the mouse position and computes the delta from the previous position.

§Arguments
  • Vector2D - The new mouse position.
Source

pub fn is_mouse_button_pressed(&self, button: MouseButton) -> bool

Tests whether a mouse button was pressed during this frame.

§Arguments
  • MouseButton - The button to check.
§Returns
  • bool - True if the button was pressed this frame.
Source

pub fn is_mouse_button_held(&self, button: MouseButton) -> bool

Tests whether a mouse button is currently held down.

§Arguments
  • MouseButton - The button to check.
§Returns
  • bool - True if the button is held.
Source

pub fn update_touch(&mut self, identifier: i32, position: Vector2D)

Adds or updates a touch point.

§Arguments
  • i32 - The touch identifier.
  • Vector2D - The touch position.
Source

pub fn start_touch(&mut self, identifier: i32, position: Vector2D)

Records a new touch point that started this frame.

§Arguments
  • i32 - The touch identifier.
  • Vector2D - The touch position.
Source

pub fn end_touch(&mut self, identifier: i32)

Removes a touch point and marks it as ended this frame.

§Arguments
  • i32 - The touch identifier.
Source

pub fn primary_touch_position(&self) -> Option<Vector2D>

Returns the position of the lowest-identifier active touch point.

Pointer-style consumers (the example pages’ interactive demos) treat the primary touch like a mouse cursor: touch events never update mouse_position, so this accessor is the only public way to read a touch position.

§Returns
  • Option<Vector2D> - The client-space position of the primary touch, or None when no touch is active.
Source

pub fn end_frame(&mut self)

Clears all per-frame input data (pressed, released, deltas).

Should be called at the end of each game frame after all input has been processed.

Source§

impl InputState

Source§

impl InputState

Source

pub fn new() -> Self

Trait Implementations§

Source§

impl Clone for InputState

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 InputState

Source§

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

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

impl Default for InputState

Implements Default for InputState as a fresh empty state.

Source§

fn default() -> InputState

Constructs a default InputState value.

§Returns
  • InputState - A default-constructed instance with the documented initial state.
Source§

impl PartialEq for InputState

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 InputState

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<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> 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> 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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more