Compare commits

..

2 Commits

Author SHA1 Message Date
paring aecd793830
feat: stop hook 2022-11-03 19:38:37 +09:00
paring 52dcfd0614
feat: start hook 2022-11-03 19:37:53 +09:00
2 changed files with 71 additions and 1 deletions

View File

@ -8,7 +8,6 @@ mkdir -p out/i686
# x86_64 # x86_64
cargo build --release --target x86_64-unknown-linux-gnu cargo build --release --target x86_64-unknown-linux-gnu
# cargo build --release --target x86_64-apple-darwin # cargo build --release --target x86_64-apple-darwin
echo [Windows]
cargo build --release --target x86_64-pc-windows-gnu cargo build --release --target x86_64-pc-windows-gnu

View File

@ -1 +1,72 @@
use std::{
ffi::{c_char, CString},
ptr::null,
time::{SystemTime, UNIX_EPOCH},
};
extern crate skyhook; extern crate skyhook;
#[repr(C)]
pub enum NativeEventType {
KeyPressed,
KeyReleased,
}
#[repr(C)]
pub struct NativeEvent {
pub time: usize,
pub event_type: NativeEventType,
pub key: u16,
}
static mut CALLBACK: Option<extern "C" fn(NativeEvent)> = None;
fn send_callback(ev: NativeEvent) {
unsafe {
if let Some(cb) = CALLBACK {
cb(ev);
}
}
}
fn get_time(time: SystemTime) -> usize {
time.duration_since(UNIX_EPOCH).unwrap().as_millis() as usize
}
#[no_mangle]
pub extern "C" fn start_hook(callback: extern "C" fn(NativeEvent)) -> *const c_char {
unsafe {
CALLBACK = Some(callback);
}
if let Err(e) = skyhook::run(move |event| {
let event = match event.data {
skyhook::types::EventData::KeyPress(key) => NativeEvent {
time: get_time(event.time),
event_type: NativeEventType::KeyPressed,
key,
},
skyhook::types::EventData::KeyRelease(key) => NativeEvent {
time: get_time(event.time),
event_type: NativeEventType::KeyReleased,
key,
},
};
send_callback(event);
}) {
let cstr = CString::new(e.message).unwrap();
return cstr.as_ptr();
}
null()
}
#[no_mangle]
pub extern "C" fn stop_hook() -> *const c_char {
if let Err(e) = skyhook::stop() {
let cstr = CString::new(e.message).unwrap();
return cstr.as_ptr();
}
null()
}