I'm new to Rust and wanted some help because I can't figure out why this simple code is not working.
I'm using rdev library and I'm just trying to store the typed key in a buffer and delete the last letter when I press backspace key.
What is weird is that the backspace key trigger correctly but nothing happen when pop()
method is called, thus I'm wondering what's wrong here?
.clear()
method seems to work correctly.
I'm using rdev = "0.5.2"
use rdev::{listen, Event, EventType, Key};
use std::sync::{Arc, Mutex};
#[derive(Default)]
struct AppState {
buffer: String,
}
fn handle_key(app_state: &mut AppState, event: &Event) {
if let Some(ref string) = event.name {
app_state.buffer.push_str(string);
}
else if let EventType::KeyRelease(key) = event.event_type {
if key == Key::Backspace {
app_state.buffer.pop();
}
if key == Key::Escape {
app_state.buffer.clear()
}
println!("buffer : {}", app_state.buffer);
}
}
#[tokio::main]
async fn main() {
let app_state = Arc::new(Mutex::new(AppState::default()));
let callback = {
move |event: Event| {
let mut app_state = app_state.lock().unwrap();
handle_key(&mut app_state, &event);
}
};
if let Err(error) = listen(callback) {
println!("Error: {:?}", error)
}
}
Thank you for you help regarding this. Also if you think there is a better way to store the pressed keys let me know. Maybe there's a way to store the key and check if the user delete not only the last key but maybe also keys that are in a middle of a word for example.