Struct winit_input_helper::WinitInputHelper
source · pub struct WinitInputHelper { /* private fields */ }
Expand description
The main struct of the API.
Create with WinitInputHelper::new
.
Call WinitInputHelper::update
for every winit::event::Event
you receive from winit.
WinitInputHelper::update
returning true indicates a step has occured.
You should now run your application logic, calling any of the accessor methods you need.
An alternative API is provided via WinitInputHelper::step_with_window_events
,
call this method instead of WinitInputHelper::update
if you need to manually control when a new step begins.
A step occurs every time this method is called.
Do not mix usages of WinitInputHelper::update
and WinitInputHelper::step_with_window_events
.
You should stick to one or the other.
Implementations§
source§impl WinitInputHelper
impl WinitInputHelper
pub fn new() -> WinitInputHelper
sourcepub fn update<T>(&mut self, event: &Event<T>) -> bool
pub fn update<T>(&mut self, event: &Event<T>) -> bool
Pass every winit event to this function and run your application logic when it returns true.
The following winit events are handled:
Event::NewEvents
clears all internal state.Event::MainEventsCleared
causes this function to return true, signifying a “step” has completed.Event::WindowEvent
updates internal state, this will affect the result of accessor methods immediately.Event::DeviceEvent
updates value ofmouse_diff()
sourcepub fn step_with_window_events(&mut self, events: &[WindowEvent])
pub fn step_with_window_events(&mut self, events: &[WindowEvent])
Pass a slice containing every winit event that occured within the step to this function.
Ensure this method is only called once per application main loop.
Ensure every event since the last WinitInputHelper::step_with_window_events
call is included in the events
argument.
WinitInputHelper::Update
is easier to use.
But this method is useful when your application logic steps dont line up with winit’s event loop.
e.g. you have a seperate thread for application logic using WinitInputHelper that constantly
runs regardless of winit’s event loop and you need to send events to it directly.
sourcepub fn key_pressed(&self, keycode: KeyCode) -> bool
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.
sourcepub fn key_pressed_os(&self, keycode: KeyCode) -> bool
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.
sourcepub fn key_released(&self, keycode: KeyCode) -> bool
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.
sourcepub fn key_held(&self, keycode: KeyCode) -> bool
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.
sourcepub fn held_shift(&self) -> bool
pub fn held_shift(&self) -> bool
Returns true while any shift key is held on the keyboard. Otherwise returns false.
Uses physical keys.
sourcepub fn held_control(&self) -> bool
pub fn held_control(&self) -> bool
Returns true while any control key is held on the keyboard. Otherwise returns false.
Uses physical keys.
sourcepub fn held_alt(&self) -> bool
pub fn held_alt(&self) -> bool
Returns true while any alt key is held on the keyboard. Otherwise returns false.
Uses physical keys.
sourcepub fn key_pressed_logical(&self, check_key: Key<&str>) -> bool
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.
sourcepub fn key_pressed_os_logical(&self, check_key: Key<&str>) -> bool
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.
sourcepub fn key_released_logical(&self, check_key: Key<&str>) -> bool
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.
sourcepub fn key_held_logical(&self, check_key: Key<&str>) -> bool
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.
sourcepub fn mouse_pressed(&self, check_mouse_button: usize) -> bool
pub fn mouse_pressed(&self, check_mouse_button: usize) -> bool
Returns true when the specified mouse button goes from “not pressed” to “pressed”. Otherwise returns false.
Left => 0 Right => 1 Middle => 2 Other => 3..255
sourcepub fn mouse_released(&self, check_mouse_button: usize) -> bool
pub fn mouse_released(&self, check_mouse_button: usize) -> bool
Returns true when the specified mouse button goes from “pressed” to “not pressed”. Otherwise returns false.
Left => 0 Right => 1 Middle => 2 Other => 3..255
sourcepub fn mouse_held(&self, mouse_button: usize) -> bool
pub fn mouse_held(&self, mouse_button: usize) -> bool
Returns true while the specified mouse button remains “pressed”. Otherwise returns false.
Left => 0 Right => 1 Middle => 2 Other => 3..255
sourcepub fn scroll_diff(&self) -> (f32, f32)
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)
sourcepub fn cursor(&self) -> Option<(f32, f32)>
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
sourcepub fn cursor_diff(&self) -> (f32, f32)
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)
.
sourcepub fn mouse_diff(&self) -> (f32, f32)
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.
Because this uses DeviceEvent
s, the step_with_windows_events
function won’t update this as it is not a WindowEvent
.
sourcepub fn text(&self) -> Vec<TextChar>
pub fn text(&self) -> Vec<TextChar>
Returns the characters pressed during the last step. The earlier the character was pressed, the lower the index in the Vec.
sourcepub fn dropped_file(&self) -> Option<PathBuf>
pub fn dropped_file(&self) -> Option<PathBuf>
Returns the path to a file that has been drag-and-dropped onto the window.
sourcepub fn window_resized(&self) -> Option<PhysicalSize<u32>>
pub fn window_resized(&self) -> Option<PhysicalSize<u32>>
Returns the current window size if it was resized during the last step.
Otherwise returns None
.
sourcepub fn resolution(&self) -> Option<(u32, u32)>
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.
sourcepub fn scale_factor_changed(&self) -> Option<f64>
pub fn scale_factor_changed(&self) -> Option<f64>
Returns the current scale factor if it was changed during the last step.
Otherwise returns None
.
sourcepub fn scale_factor(&self) -> Option<f64>
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.
sourcepub fn destroyed(&self) -> bool
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.
sourcepub fn close_requested(&self) -> bool
pub fn close_requested(&self) -> bool
Returns true if the OS has requested the application to close during this step. Otherwise returns false.
sourcepub fn quit(&self) -> bool
👎Deprecated: Instead use input.close_requested() || input.destroyed()
pub fn quit(&self) -> bool
input.close_requested() || input.destroyed()
Deprecated
sourcepub fn delta_time(&self) -> Option<Duration>
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
impl Clone for WinitInputHelper
source§fn clone(&self) -> WinitInputHelper
fn clone(&self) -> WinitInputHelper
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl RefUnwindSafe for WinitInputHelper
impl Send for WinitInputHelper
impl Sync for WinitInputHelper
impl Unpin for WinitInputHelper
impl UnwindSafe for WinitInputHelper
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.