jenslog-rs/src/key/keystate.rs

96 lines
2.7 KiB
Rust

use std::ffi::OsString;
use winapi::um::winuser::{GetKeyNameTextW, KBDLLHOOKSTRUCT, LLKHF_EXTENDED, LLKHF_UP};
use wio::wide::FromWide;
use crate::FlagOperations;
#[allow(clippy::struct_excessive_bools)]
pub struct KeyState {
pub kbdllstruct: KBDLLHOOKSTRUCT,
pub shift_down: bool,
pub ctrl_down: bool,
pub win_down: bool,
pub alt_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,
alt_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) {
let key_down = !key.flags.get_flag(LLKHF_UP);
self.kbdllstruct = key;
match key.vkCode {
160 | 161 => self.shift_down = key_down,
162 | 163 => self.ctrl_down = key_down,
164 | 165 => self.alt_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..=165 | 91)
}
#[no_mangle] //required to not get garbage from native call
pub fn name(&self) -> String {
unsafe {
let flags = if self.kbdllstruct.flags.get_flag(LLKHF_EXTENDED) { 1 << 24 } else { 0 };
let mut out = [0_u16; 128];
GetKeyNameTextW((self.kbdllstruct.scanCode << 16 | flags /*this distinguishes special keys by setting a flag. yes only microsoft thinks that input and flags in 1 param is a good idea*/) as i32, (&mut out).as_mut_ptr(), 128);
let null_pos = out.iter().position(|x| *x == b'\0' as u16).unwrap_or_else(|| out.len());
OsString::from_wide(&out[..null_pos]).to_str().unwrap().to_owned()
}
}
///true if this event is keydown, otherwise false
pub fn is_down(&self) -> bool {
!self.kbdllstruct.flags.get_flag(LLKHF_UP)
}
}
#[cfg(test)]
mod tests {
use crate::key::keystate::KeyState;
use winapi::um::winuser::LLKHF_UP;
#[test]
fn is_aux_key() {
let mut k = KeyState::new();
k.kbdllstruct.vkCode = 160;
assert!(k.is_aux_key());
}
#[test]
fn is_down() {
let mut k = KeyState::new();
k.kbdllstruct.flags = LLKHF_UP;
assert!(!k.is_down());
k.kbdllstruct.flags = 0;
assert!(k.is_down());
}
}