modify input module to handle special characters like escape

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-09 21:33:34 +02:00
parent 69d44cda4e
commit 3beddd9acd
2 changed files with 37 additions and 55 deletions

View file

@ -164,7 +164,18 @@ where
/// Handle the input /// Handle the input
fn handle_input(&mut self) { fn handle_input(&mut self) {
self.last_key_pressed = self.input_module.get_key_pressed(); if let Some(key_pressed) = self.input_module.get_key_pressed() {
if key_pressed == 0xFF {
// Exit requested
self.display.clear();
println!("Thank you for playing! See you next time! :-)");
std::process::exit(0);
} else {
self.last_key_pressed = Option::from((key_pressed & 0xF) as u8)
}
} else {
self.last_key_pressed = None;
}
} }
/// Should make an audible beep. /// Should make an audible beep.

View file

@ -6,14 +6,14 @@ use std::time::Duration;
pub trait InputModule { pub trait InputModule {
/// Returns the key value of the corresponding pressed key. /// Returns the key value of the corresponding pressed key.
/// None if no key is pressed. /// None if no key is pressed.
fn get_key_pressed(&mut self) -> Option<u8>; fn get_key_pressed(&mut self) -> Option<u16>;
} }
/// NoInput always returns none when queried for input. /// NoInput always returns none when queried for input.
pub struct NoInput; pub struct NoInput;
impl InputModule for NoInput { impl InputModule for NoInput {
fn get_key_pressed(&mut self) -> Option<u8> { fn get_key_pressed(&mut self) -> Option<u16> {
None None
} }
} }
@ -37,7 +37,7 @@ impl Default for CrossTermInput {
} }
impl InputModule for CrossTermInput { impl InputModule for CrossTermInput {
fn get_key_pressed(&mut self) -> Option<u8> { fn get_key_pressed(&mut self) -> Option<u16> {
if !self.initialized { if !self.initialized {
panic!("CrossTermInput needs to be constructed using ::new") panic!("CrossTermInput needs to be constructed using ::new")
} }
@ -48,60 +48,31 @@ impl InputModule for CrossTermInput {
if let Ok(event) = read_result { if let Ok(event) = read_result {
match event { match event {
Event::Key(key_event) => match key_event.code { Event::Key(key_event) => match key_event.code {
KeyCode::Esc => {
return Some(0xFF);
},
KeyCode::Char(character) => { KeyCode::Char(character) => {
let lowercase_character = character.to_lowercase(); let lowercase_character = character.to_lowercase();
for char in lowercase_character { for char in lowercase_character {
match char { return match char {
'1' => { '1' => Some(1),
return Some(1); '2' => Some(2),
} '3' => Some(3),
'2' => { '4' => Some(0xC),
return Some(2); 'q' => Some(4),
} 'w' => Some(5),
'3' => { 'e' => Some(6),
return Some(3); 'r' => Some(0xD),
} 'a' => Some(7),
'4' => { 's' => Some(8),
return Some(0xC); 'd' => Some(9),
} 'f' => Some(0xE),
'q' => { 'z' => Some(0xA),
return Some(4); 'x' => Some(0),
} 'c' => Some(0xB),
'w' => { 'v' => Some(0xF),
return Some(5); _ => None,
} };
'e' => {
return Some(6);
}
'r' => {
return Some(0xD);
}
'a' => {
return Some(7);
}
's' => {
return Some(8);
}
'd' => {
return Some(9);
}
'f' => {
return Some(0xE);
}
'z' => {
return Some(0xA);
}
'x' => {
return Some(0);
}
'c' => {
return Some(0xB);
}
'v' => {
return Some(0xF);
}
_ => {}
}
} }
} }
_ => {} _ => {}