30 lines
867 B
Rust
30 lines
867 B
Rust
use std::ptr::null_mut;
|
|
|
|
use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA, GetKeyNameTextW};
|
|
use std::ffi::OsString;
|
|
use wio::wide::FromWide;
|
|
|
|
fn main() {
|
|
unsafe {
|
|
SetWindowsHookExA(13, Some(hook_callback), null_mut(), 0);
|
|
|
|
GetMessageW(null_mut(), null_mut(), 0, 0);
|
|
}
|
|
}
|
|
|
|
unsafe extern "system" fn hook_callback(_code: i32, w_param: usize, l_param: isize) -> isize {
|
|
//Check if keydown
|
|
if w_param != 256 { return 0; }
|
|
let key = *(l_param as PKBDLLHOOKSTRUCT);
|
|
|
|
println!("{} >> {}", key.vkCode, scan_code_to_key_name(key.scanCode));
|
|
0
|
|
}
|
|
|
|
fn scan_code_to_key_name(scan_code: u32) -> String {
|
|
unsafe {
|
|
let mut out = [0u16; 128];
|
|
GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 1024);
|
|
OsString::from_wide(&out).to_str().unwrap().replace('\0', "")
|
|
}
|
|
} |