Compare commits
No commits in common. "aecd7938304c615802437fe861787940c2ebd485" and "8b7741866ade2f93e5616b119167a63d13d12320" have entirely different histories.
aecd793830
...
8b7741866a
1
build.sh
1
build.sh
|
@ -8,6 +8,7 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
|
71
src/lib.rs
71
src/lib.rs
|
@ -1,72 +1 @@
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
Reference in New Issue