4 releases (2 breaking)
0.3.1 | Dec 23, 2020 |
---|---|
0.3.0 |
|
0.2.0 | Jul 12, 2020 |
0.1.1 | Jan 14, 2020 |
0.1.0 | Jan 1, 2019 |
#16 in Games
24KB
564 lines
buttons
A simple Rust crate for managing and querying input state.
Usage
With winit
(Enabling the winit-support
feature.)
let mut keyboard = buttons::winit_support::keyboard();
let mut mouse = buttons::winit_support::mouse();
{
let mut keyboard_input = keyboard.begin_frame_input();
let mut mouse_input = mouse.begin_frame_input();
events_loop.poll_events(|event| {
if let Event::WindowEvent { event, .. } = event {
keyboard_input.handle_event(&event);
mouse_input.handle_event(&event);
}
});
}
if keyboard.pressed(VirtualKeyCode::Escape) || mouse.released(MouseButton::Right) {
...
}
lib.rs
:
This is simple Rust crate for managing and querying input state.
It treats the mouse and keyboard as an immutable data structure that you can query to find which keys and buttons are pressed (or where the pointer is).
The core data structures of Mouse
and Keyboard
are generic, in theory
supporting multiple windowing libraries. You can roll your own, or you can
enable the winit-support
which has factory methods to easily create
a Mouse
and Keyboard
which work with the winit
library.
As stated, the mouse and keyboard are immutable. To track input changes,
each provide a begin_frame_input
method which return an object you can
make changes to for the frame.
Examples
# #[cfg(feature = "winit_support")] {
# use winit::{Event, VirtualKeyCode, MouseButton};
let mut keyboard = buttons::winit_support::keyboard();
let mut mouse = buttons::winit_support::mouse();
// Track input
{
let mut keyboard_input = keyboard.begin_frame_input();
let mut mouse_input = mouse.begin_frame_input();
events_loop.poll_events(|event| {
if let Event::WindowEvent { event, .. } = event {
keyboard_input.handle_event(&event);
mouse_input.handle_event(&event);
}
});
}
// Check state
if keyboard.pressed(VirtualKeyCode::Escape) || mouse.released(MouseButton::Right) {
// Do something
}
# }
Dependencies
~0–1.3MB
~31K SLoC