fix: check if key is pressed on remove_key
parent
b601aca4df
commit
cb001dbf95
|
|
@ -1,8 +1,11 @@
|
||||||
use std::{time::SystemTime, collections::HashSet};
|
use std::{collections::HashSet, time::SystemTime};
|
||||||
|
|
||||||
use winsafe::{co::WM, prelude::user_Hhook, HHOOK};
|
use winsafe::{co::WM, prelude::user_Hhook, HHOOK};
|
||||||
|
|
||||||
use crate::{types::{Event, EventData}, breakable_unsafe};
|
use crate::{
|
||||||
|
breakable_unsafe,
|
||||||
|
types::{Event, EventData},
|
||||||
|
};
|
||||||
|
|
||||||
use super::{CALLBACK, KBD_HOOK_ID};
|
use super::{CALLBACK, KBD_HOOK_ID};
|
||||||
|
|
||||||
|
|
@ -36,10 +39,11 @@ unsafe fn add_key(key: u16) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn remove_key(key: u16) {
|
unsafe fn remove_key(key: u16) -> bool {
|
||||||
if let Some(keys) = PRESSED_KEYS.as_mut() {
|
if let Some(keys) = PRESSED_KEYS.as_mut() {
|
||||||
keys.remove(&key);
|
return keys.remove(&key);
|
||||||
}
|
}
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub extern "system" fn hook_callback(code: i32, wparam: usize, lparam: isize) -> isize {
|
pub extern "system" fn hook_callback(code: i32, wparam: usize, lparam: isize) -> isize {
|
||||||
|
|
@ -64,18 +68,20 @@ pub extern "system" fn hook_callback(code: i32, wparam: usize, lparam: isize) ->
|
||||||
time: SystemTime::now(),
|
time: SystemTime::now(),
|
||||||
data: EventData::KeyPress(vkcode),
|
data: EventData::KeyPress(vkcode),
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
WM::KEYUP | WM::SYSKEYUP => {
|
WM::KEYUP | WM::SYSKEYUP => {
|
||||||
let vkcode = get_code(lparam) as u16;
|
let vkcode = get_code(lparam) as u16;
|
||||||
|
|
||||||
// Do not ignore lifted keys upon next down event
|
// Do not ignore lifted keys upon next down event
|
||||||
remove_key(vkcode);
|
if !remove_key(vkcode) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
CALLBACK.unwrap()(Event {
|
CALLBACK.unwrap()(Event {
|
||||||
time: SystemTime::now(),
|
time: SystemTime::now(),
|
||||||
data: EventData::KeyRelease(vkcode),
|
data: EventData::KeyRelease(vkcode),
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Reference in New Issue