WinitInputHelper

Struct WinitInputHelper 

Source
pub struct WinitInputHelper { /* private fields */ }
Expand description

The main struct of the API.

Create with WinitInputHelper::new.

  • Call WinitInputHelper::process_window_event() for every window event you recieve in ApplicationHandler.window_event()
  • Call WinitInputHelper::process_device_event() every time ApplicationHandler.device_event() is called.
  • Call WinitInputHelper::step() every time ApplicationHandler.new_events() is called.
  • Call WinitInputHelper::end_step() every time ApplicationHandler.about_to_wait() is called.

It is crucial that you call all of these functions every time they are required to be called:

  • failing to call new_events() or about_to_wait() will break the separation between frames
  • failing to call device_event() will prevent mouse motion from being detected in some cases.

WinitInputHelper::process_window_event() returning true indicates a RequestedRedraw event was received, and that you should render.

You should be running your application logic only in ApplicationHandler.about_to_wait(), calling any of the accessor methods you need. All the window events should have been registered beforehand by calls of WinitInputHelper::process_window_event().

Implementations§

Source§

impl WinitInputHelper

Source

pub fn new() -> WinitInputHelper

Source

pub fn step(&mut self)

Call every time ApplicationHandler.new_events() is called. Clears all internal state.

Source

pub fn process_window_event(&mut self, event: &WindowEvent) -> bool

Call every time ApplicationHandler.window_event() is called. Updates internal state, this will affect the result of accessor methods immediately. You should render your application only when this function returns true, which is exactly and only when a RedrawRequested event is received. If you want to render every frame, call window.request_redraw() on the relevant window every time ApplicationHandler.about_to_wait() is called. For more information on when to render, see Window::request_redraw() in the winit docs. It is important to note that this method does not care which window the RedrawRequested event comes from (i.e. the WindowId). If you want to only redraw the window that was requested to be redrawn, you should check for RedrawRequested events yourself.

Source

pub fn process_device_event(&mut self, event: &DeviceEvent)

Call every time ApplicationHandler.device_event() is called. Updates value of mouse_diff().

Source

pub fn end_step(&mut self)

Source

pub fn key_pressed(&self, keycode: KeyCode) -> bool

Returns true when the key with the specified keycode goes from “not pressed” to “pressed”. Otherwise returns false.

Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.

This is suitable for game controls.

Source

pub fn key_pressed_os(&self, keycode: KeyCode) -> bool

Returns true when the key with the specified keycode goes from “not pressed” to “pressed”. Otherwise returns false.

Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.

Will repeat key presses while held down according to the OS’s key repeat configuration This is suitable for UI.

Source

pub fn key_released(&self, keycode: KeyCode) -> bool

Returns true when the key with the specified KeyCode goes from “pressed” to “not pressed”. Otherwise returns false.

Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.

Source

pub fn key_held(&self, keycode: KeyCode) -> bool

Returns true when the key with the specified keycode remains “pressed”. Otherwise returns false.

Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.

Source

pub fn held_shift(&self) -> bool

Returns true while any shift key is held on the keyboard. Otherwise returns false.

Uses physical keys.

Source

pub fn held_control(&self) -> bool

Returns true while any control key is held on the keyboard. Otherwise returns false.

Uses physical keys.

Source

pub fn held_alt(&self) -> bool

Returns true while any alt key is held on the keyboard. Otherwise returns false.

Uses physical keys.

Source

pub fn key_pressed_logical(&self, check_key: Key<&str>) -> bool

Returns true when the specified keyboard key goes from “not pressed” to “pressed”. Otherwise returns false.

Uses logical keypresses, so for example W is changed between a US and french keyboard. Will never repeat keypresses while held.

Source

pub fn key_pressed_os_logical(&self, check_key: Key<&str>) -> bool

Returns true when the specified keyboard key goes from “not pressed” to “pressed”. Otherwise returns false.

Uses logical keypresses, so for example W is changed between a US and french keyboard.

Will repeat key presses while held down according to the OS’s key repeat configuration This is suitable for UI.

Source

pub fn key_released_logical(&self, check_key: Key<&str>) -> bool

Returns true when the specified keyboard key goes from “pressed” to “not pressed”. Otherwise returns false.

Uses logical keypresses, so for example W is changed between a US and french keyboard.

Source

pub fn key_held_logical(&self, check_key: Key<&str>) -> bool

Returns true while the specified keyboard key remains “pressed”. Otherwise returns false.

Uses logical keypresses, so for example W is changed between a US and french keyboard.

Source

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

Returns true when the specified mouse button goes from “not pressed” to “pressed”. Otherwise returns false.

Source

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

Returns true when the specified mouse button goes from “pressed” to “not pressed”. Otherwise returns false.

Source

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

Returns true while the specified mouse button remains “pressed”. Otherwise returns false.

Source

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

Returns (0.0, 0.0) when the window is not focused. Otherwise returns the amount scrolled by the mouse during the last step. Returns (horizontally, vertically)

Source

pub fn cursor(&self) -> Option<(f32, f32)>

Returns the cursor coordinates in pixels, when window is focused AND (cursor is on window OR any mouse button remains held while cursor moved off window) Otherwise returns None

Source

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

Returns the change in cursor coordinates that occured during the last step, when window is focused AND (cursor is on window OR any mouse button remains held while cursor moved off window) Otherwise returns (0.0, 0.0).

Source

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

Returns the change in mouse coordinates that occured during the last step.

This is useful when implementing first person controls with a captured mouse.

Source

pub fn text(&self) -> &[Key]

Returns the characters pressed during the last step. The characters are in the order they were pressed.

Source

pub fn dropped_file(&self) -> Option<PathBuf>

Returns the path to a file that has been drag-and-dropped onto the window.

Source

pub fn window_resized(&self) -> Option<PhysicalSize<u32>>

Returns the current window size if it was resized during the last step. Otherwise returns None.

Source

pub fn resolution(&self) -> Option<(u32, u32)>

Returns None when no WindowEvent::Resized have been received yet. After one has been received it returns the current resolution of the window.

Source

pub fn scale_factor_changed(&self) -> Option<f64>

Returns the current scale factor if it was changed during the last step. Otherwise returns None.

Source

pub fn scale_factor(&self) -> Option<f64>

Returns None when no WindowEvent::ScaleFactorChanged have been received yet. After one has been received it returns the current scale_factor of the window.

Source

pub fn destroyed(&self) -> bool

Returns true if the window has been destroyed Otherwise returns false. Once this method has returned true once all following calls to this method will also return true.

Source

pub fn close_requested(&self) -> bool

Returns true if the OS has requested the application to close during this step. Otherwise returns false.

Source

pub fn delta_time(&self) -> Option<Duration>

Returns the std::time::Duration elapsed since the last step. Returns None if the step is still in progress.

Trait Implementations§

Source§

impl Clone for WinitInputHelper

Source§

fn clone(&self) -> WinitInputHelper

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Default for WinitInputHelper

Source§

fn default() -> Self

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

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> 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> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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