Curses key release is equally troublesome. Will revisit, but this generates the events - but doesn't handle holding a key properly.
@@ -16,6 +16,8 @@ pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> Result<(
let mut frames = 0;
let mut button_map : HashSet<usize> = HashSet::new();
+ let mut key_map : HashSet<char> = HashSet::new();
+ let mut keys_this_frame : HashSet<char> = HashSet::new();
while !bterm.quitting {
let now_seconds = now.elapsed().as_secs();
@@ -36,13 +38,19 @@ pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> Result<(
// Input
clear_input_state(&mut bterm);
let mut buttons_this_frame = (false, false, false);
+ keys_this_frame.clear();
+ loop {
let input = BACKEND.lock().unwrap().window.as_ref().unwrap().getch();
if let Some(input) = input {
match input {
pancurses::Input::Character(c) => {
+ keys_this_frame.insert(c);
+ if !key_map.contains(&c) {
bterm.on_event(BEvent::Character { c });
if let Some(key) = char_to_keycode(c) {
bterm.on_key(key, 0, true); // How do I get the scancode?
+ key_map.insert(c);
+ }
}
}
pancurses::Input::KeyLeft => bterm.on_key(VirtualKeyCode::Left, 0, true),
@@ -83,12 +91,26 @@ pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> Result<(
println!("{:#?}", input);
}
}
+ } else {
+ break;
+ }
}
if !buttons_this_frame.0 && button_map.contains(&0) {
button_map.remove(&0);
bterm.on_mouse_button(0, false);
}
+ let keys_released = key_map
+ .iter()
+ .filter(|k| !keys_this_frame.contains(k))
+ .map(|k| *k)
+ .collect::<Vec<char>>();
+ for key in keys_released {
+ key_map.remove(&key);
+ if let Some(key) = char_to_keycode(key) {
+ bterm.on_key(key, 0, false);
+ }
+ }
gamestate.tick(&mut bterm);
