Skip to main content

Input

Struct Input 

Source
pub struct Input { /* private fields */ }

Implementations§

Source§

impl Input

Source

pub fn new() -> Self

Source

pub fn gamepads(&self) -> impl Iterator<Item = &Gamepad>

Iterator over currently connected gamepads.

Source

pub fn first_gamepad(&self) -> Option<&Gamepad>

The first connected gamepad, if any. Convenient for single-player input.

Source

pub fn gamepad(&self, id: GamepadId) -> Option<&Gamepad>

Look up a connected gamepad by id.

Source

pub fn poll_gamepads(&mut self)

Drain pending gamepad events into per-pad state, tracking hot-plug. Call once per frame before update.

Source

pub fn set_rumble(&mut self, id: GamepadId, magnitude: f32, duration_ms: u32)

Rumble id at magnitude (0..=1) for duration_ms. No-op when the pad has no force feedback or no backend is available.

Source

pub fn key_held(&self, k: Key) -> bool

Source

pub fn key_pressed(&self, k: Key) -> bool

Source

pub fn key_released(&self, k: Key) -> bool

Source

pub fn any_key_pressed(&self) -> bool

Any key other than a modifier (Shift, Ctrl, Alt, Super, …) went down this frame: the classic “press any key”. Modifiers alone don’t count, so Alt-Tabbing away or holding Shift doesn’t skip a screen; Input::keys_pressed still reports them.

Source

pub fn keys_pressed(&self) -> impl Iterator<Item = Key> + '_

Every key that went down this frame, modifiers included, in no particular order (for “press a key to bind it”, cheat codes, typing a name).

Source

pub fn any_mouse_pressed(&self) -> bool

Any mouse button went down this frame.

Source

pub fn any_pressed(&self) -> bool

Anything at all went down this frame: a non-modifier key (Input::any_key_pressed), a mouse button, a button on any connected gamepad, or a finger touching the screen (Input::any_touch_pressed). For “press any key to continue”, skipping intros, and waking an idle game.

use game_toolkit_input::Input;

enum Screen { Title, Playing }

fn update(input: &Input, screen: &mut Screen) {
    if matches!(screen, Screen::Title) && input.any_pressed() {
        *screen = Screen::Playing;
    }
}

let mut screen = Screen::Title;
update(&Input::default(), &mut screen); // nothing pressed yet
assert!(matches!(screen, Screen::Title));
Source

pub fn touches(&self) -> impl Iterator<Item = &Touch>

Fingers currently on the screen, in no particular order.

Source

pub fn touch(&self, id: u64) -> Option<&Touch>

The finger with this Touch::id, if it is still down.

Source

pub fn touches_started(&self) -> &[Touch]

Fingers that went down this frame, in event order. A tap that starts and ends within one frame shows up here and in Input::touches_ended, but not in Input::touches.

Source

pub fn touches_ended(&self) -> &[Touch]

Fingers that lifted this frame, in event order. Touches the platform cancels (e.g. the browser took over the gesture, or the window lost focus) vanish from Input::touches without appearing here, so a cancelled touch never counts as a tap.

Source

pub fn any_touch_pressed(&self) -> bool

Any finger went down this frame.

Source

pub fn set_touch_emulates_mouse(&mut self, on: bool)

Let the first finger on a touchscreen act as the mouse, so a game written for the mouse also works on a phone. Off by default (also settable through AppConfig::touch_emulates_mouse).

While on, the first finger down moves Input::mouse_pos to where it touched (without a Input::mouse_delta jump), presses and holds MouseButton::Left, drags the cursor along, and releases the button where it lifts. Other fingers are ignored until it lifts. A cancelled touch lets go of the button without a Input::mouse_released edge, so it never counts as a click. Input::touches and friends keep working.

No effect on Windows: the OS already turns touches into mouse events there, which winit passes on, so emulating them again would press twice.

Source

pub fn touch_emulates_mouse(&self) -> bool

Source

pub fn mouse_held(&self, b: MouseButton) -> bool

Source

pub fn mouse_pressed(&self, b: MouseButton) -> bool

Source

pub fn mouse_released(&self, b: MouseButton) -> bool

Source

pub fn mouse_pos(&self) -> (f32, f32)

Source

pub fn mouse_delta(&self) -> (f32, f32)

Source

pub fn scroll(&self) -> (f32, f32)

Source

pub fn handle_window_event(&mut self, event: &WindowEvent)

Source

pub fn end_frame(&mut self)

Call once per frame after update to clear edge / delta state.

Trait Implementations§

Source§

impl Default for Input

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Sync for Input

§

impl Freeze for Input

§

impl RefUnwindSafe for Input

§

impl Send for Input

§

impl Unpin for Input

§

impl UnsafeUnpin for Input

§

impl UnwindSafe for Input

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more