jenslog-rs/src/key/keystate.rs
LordMZTE 9e78b067f5 move keystate into own file
configure clippy linter
adjust style to linter settings
2020-08-26 13:51:43 +02:00

43 lines
1.2 KiB
Rust

use winapi::um::winuser::KBDLLHOOKSTRUCT;
pub struct KeyState {
pub kbdllstruct: KBDLLHOOKSTRUCT,
pub shift_down: bool,
pub ctrl_down: bool,
pub win_down: bool,
}
impl KeyState {
pub fn new() -> Box<Self> {
Box::new(Self {
kbdllstruct: KBDLLHOOKSTRUCT {
vkCode: 0,
scanCode: 0,
flags: 0,
time: 0,
dwExtraInfo: 0,
},
shift_down: false,
ctrl_down: false,
win_down: false,
})
}
/// `key_down` if the event was a keydown event, this should be true. if it was keyup it should be false
pub fn update(&mut self, key: KBDLLHOOKSTRUCT, key_down: bool) {
self.kbdllstruct = key;
match key.vkCode {
160 | 161 => self.shift_down = key_down,
162 | 163 => self.ctrl_down = key_down,
91 => self.win_down = key_down,
_ => {}
}
}
/// true if the key is an auxiliary key like shift, control or the windows key
pub fn is_aux_key(&self) -> bool {
matches!(self.kbdllstruct.vkCode, 160 | 161 | 162 | 163 | 91)
}
}