Getting Input
Currently, the ndless
crate supports input from the keyboard and
touchpad. The input module contains these functions.
Use get_keys
to get a Vec
of Key
s that are currently being
pressed. An empty Vec
will be returned if no keys are currently
pressed. See the following example:
use ndless::input::{get_keys, Key};
let keys = get_keys();
if keys.len() == 0 { /* No keys currently pressed */ }
However, a more efficient way is to use iter_keys
, which does not
allocate. However, it must be used immediately, as each iteration of the
loop checks if the key is being pressed at that time. For example:
use ndless::prelude::*;
use ndless::input::iter_keys;
for key in iter_keys() {
println!("Key {:?} is being pressed.", key);
}
Additionally, it may be used like any other Iterator
in Rust:
// Print all keys except escape
use ndless::prelude::*;
use ndless::input::{iter_keys, Key};
iter_keys()
.filter(|key| key != Key::Esc)
.for_each(|key| println!("Key {:?} is being pressed.", key));
Simple (boolean) functions
Description | Function |
---|---|
Returns true if the specific key is pressed. Note that you may pass either an owned Key or a borrowed &Key . | is_key_pressed |
Returns true if any buttons are currently pressed, including pushing the touchpad. | any_key_pressed |
Returns true if the "On" key is currently pressed. | key_on_pressed |
Suspends the program until any_key_pressed returns true. | wait_key_pressed |
Suspends the program until any_key_pressed returns false. | wait_no_key_pressed |