Struct console_input::keypress::Input
source · #[non_exhaustive]pub struct Input {
pub frame_skip: bool,
pub event: Option<Event>,
}Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.frame_skip: boolReturns true if the next frame should be skipped due to the previous frame taking too long
event: Option<Event>Implementations§
source§impl Input
impl Input
sourcepub fn read() -> Self
pub fn read() -> Self
Wait to get an input. When raw mode is disabled, the input will only be registered after enter is pressed. Call enable_raw_mode() to avoid having to press enter
Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() {
keypress::enable_raw_mode();
loop {
if let Some(Event::Key(key_event)) = Input::read().exit_on_kb_interrupt().event {
match key_event {
KeyEvent {
code: KeyCode::Char(char),
..
} => printr!("You pressed the {} key!", char),
KeyEvent {
code: KeyCode::Enter,
kind: KeyEventKind::Press,
..
} => printr!("You pressed enter!"),
_ => (),
}
}
}
}sourcepub fn sleep_for_input(dur: Duration) -> Self
pub fn sleep_for_input(dur: Duration) -> Self
Spend the given duration attempted to get a keyboard press. Returns None if no event found within time limit. This function will always take the given duration to execute, even if an event is captured sooner than intended
sourcepub fn sleep_fps_and_get_input(fps: f32, elapsed: Duration) -> Self
pub fn sleep_fps_and_get_input(fps: f32, elapsed: Duration) -> Self
sleep for 1/fps given the passed fps argument, taking away the pass elapsed value to account for however long it took your gameloop to run. The returned bool is true if elapsed was more than 1/fps, in which case you should attempt to skip the next frame the best that you can
This function is intended to be inserted into MainLoopRoot::sleep_and_get_input_data in gemini-engine
sourcepub fn as_tuple(&self) -> (bool, Option<Event>)
pub fn as_tuple(&self) -> (bool, Option<Event>)
Generate a tuple from the input. Ideal to plug into gemini-engine’s MainLoopRoot::sleep_and_get_input_data
sourcepub fn exit_on_kb_interrupt(self) -> Self
pub fn exit_on_kb_interrupt(self) -> Self
If this input was a Ctrl+C (Keyboard Interrupt), exit immediately. Consumes the original Input but returns it immediately, so is intended to be used like this:
// Will get the input but exit the process if the input was `Ctrl+C`
let input = Input::read()
.exit_on_kb_interrupt();Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() {
keypress::enable_raw_mode();
loop {
if let Some(Event::Key(key_event)) = Input::read().exit_on_kb_interrupt().event {
match key_event {
KeyEvent {
code: KeyCode::Char(char),
..
} => printr!("You pressed the {} key!", char),
KeyEvent {
code: KeyCode::Enter,
kind: KeyEventKind::Press,
..
} => printr!("You pressed enter!"),
_ => (),
}
}
}
}