Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::Vec2;
use crate::{get_context, DroppedFile};
pub use miniquad::{KeyCode, MouseButton};

const KEY_REPEAT_DELAY: f64 = 5.0;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TouchPhase {
Started,
Expand Down Expand Up @@ -132,6 +134,26 @@ pub fn is_key_released(key_code: KeyCode) -> bool {
context.keys_released.contains(&key_code)
}

/// Detect if the key has been pressed for some time
pub fn is_key_repeated(key_code: KeyCode) -> bool {
let context = get_context();

// stores the amount of time each key has been pressed for
let time_map = &mut context.keys_repeated;

time_map.entry(key_code).or_insert(0.);

let time_pressed = time_map.get_mut(&key_code).unwrap();

if is_key_down(key_code) {
*time_pressed += 1.;
} else {
*time_pressed = 0.;
}

*time_pressed >= KEY_REPEAT_DELAY
}

/// Return the last pressed char.
/// Each "get_char_pressed" call will consume a character from the input queue.
pub fn get_char_pressed() -> Option<char> {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ struct Context {
keys_down: HashSet<KeyCode>,
keys_pressed: HashSet<KeyCode>,
keys_released: HashSet<KeyCode>,
keys_repeated: HashMap<KeyCode, f64>,
mouse_down: HashSet<MouseButton>,
mouse_pressed: HashSet<MouseButton>,
mouse_released: HashSet<MouseButton>,
Expand Down Expand Up @@ -321,6 +322,7 @@ impl Context {
keys_down: HashSet::new(),
keys_pressed: HashSet::new(),
keys_released: HashSet::new(),
keys_repeated: HashMap::new(),
chars_pressed_queue: Vec::new(),
chars_pressed_ui_queue: Vec::new(),
mouse_down: HashSet::new(),
Expand Down