41 lines
922 B
Rust
41 lines
922 B
Rust
#![macro_use]
|
|
|
|
use winapi::um::winuser::{HOOKPROC, SetWindowsHookExA, GetMessageW};
|
|
use std::ptr::null_mut;
|
|
|
|
#[macro_export]
|
|
//im suprised this works.
|
|
macro_rules! hookproc {
|
|
($i:expr) => {{
|
|
use std::option::Option::Some;
|
|
use winapi::um::winuser::PKBDLLHOOKSTRUCT;
|
|
|
|
unsafe extern "system" fn hookproc(code: i32, w_param: usize, l_param: isize) -> isize {
|
|
$i(code, w_param, *(l_param as PKBDLLHOOKSTRUCT));
|
|
0
|
|
}
|
|
|
|
Some(hookproc)
|
|
}}
|
|
}
|
|
|
|
pub struct Keyhook {
|
|
pub callback: HOOKPROC,
|
|
}
|
|
|
|
impl Keyhook {
|
|
/// use `hookproc` macro here to get callback from closure
|
|
pub fn new(callback: HOOKPROC) -> Self {
|
|
Self {
|
|
callback,
|
|
}
|
|
}
|
|
|
|
pub fn start(&self) {
|
|
unsafe {
|
|
SetWindowsHookExA(13, self.callback.clone(), null_mut(), 0);
|
|
GetMessageW(null_mut(), null_mut(), 0, 0);
|
|
}
|
|
}
|
|
}
|