feat: start hook
parent
8b7741866a
commit
52dcfd0614
1
build.sh
1
build.sh
|
@ -8,7 +8,6 @@ mkdir -p out/i686
|
|||
# x86_64
|
||||
cargo build --release --target x86_64-unknown-linux-gnu
|
||||
# cargo build --release --target x86_64-apple-darwin
|
||||
echo [Windows]
|
||||
cargo build --release --target x86_64-pc-windows-gnu
|
||||
|
||||
|
||||
|
|
64
src/lib.rs
64
src/lib.rs
|
@ -1 +1,65 @@
|
|||
use std::{
|
||||
ffi::{c_char, CString},
|
||||
ptr::null,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
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() {}
|
||||
|
|
Reference in New Issue