这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! # Listening to global events
//!
//! ```no_run
//! use rdev::{listen, Event};
//! use rdev::{listen, Event, stop_listen};
//!
//! // This will block.
//! if let Err(error) = listen(callback) {
Expand Down Expand Up @@ -228,7 +228,10 @@ mod macos;
#[cfg(target_os = "macos")]
pub use crate::macos::Keyboard;
#[cfg(target_os = "macos")]
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
use crate::macos::{
display_size as _display_size, listen as _listen, simulate as _simulate,
stop_listen as _stop_listen,
};

#[cfg(target_os = "linux")]
mod linux;
Expand All @@ -242,7 +245,10 @@ mod windows;
#[cfg(target_os = "windows")]
pub use crate::windows::Keyboard;
#[cfg(target_os = "windows")]
use crate::windows::{display_size as _display_size, listen as _listen, simulate as _simulate};
use crate::windows::{
display_size as _display_size, listen as _listen, simulate as _simulate,
stop_listen as _stop_listen,
};

/// Listening to global events. Caveat: On MacOS, you require the listen
/// loop needs to be the primary app (no fork before) and need to have accessibility
Expand Down Expand Up @@ -272,6 +278,10 @@ where
_listen(callback)
}

pub fn stop_listen() {
_stop_listen()
}

/// Sending some events
///
/// ```no_run
Expand Down
3 changes: 1 addition & 2 deletions src/macos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ extern "C" {
pub fn CFRunLoopGetCurrent() -> CFRunLoopRef;
pub fn CGEventTapEnable(tap: CFMachPortRef, enable: bool);
pub fn CFRunLoopRun();

pub fn CFRunLoopStop(rl: CFRunLoopRef);
pub static kCFRunLoopCommonModes: CFRunLoopMode;

}

// TODO Remove this, this was added as the coded
Expand Down
18 changes: 15 additions & 3 deletions src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ unsafe extern "C" fn raw_callback(
cg_event
}

pub fn stop_listen() {
unsafe {
if let Some(stop_loop) = STOP_LOOP.as_ref() {
stop_loop();
}
}
}

pub fn listen<T>(callback: T) -> Result<(), ListenError>
where
T: FnMut(Event) + 'static,
Expand All @@ -50,16 +58,20 @@ where
if tap.is_null() {
return Err(ListenError::EventTapError);
}
let _loop = CFMachPortCreateRunLoopSource(nil, tap, 0);
let _loop: CFRunLoopSourceRef = CFMachPortCreateRunLoopSource(nil, tap, 0);
if _loop.is_null() {
return Err(ListenError::LoopSourceError);
}

let current_loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);

CGEventTapEnable(tap, true);
STOP_LOOP = Some(Box::new(move || {
CFRunLoopStop(current_loop);
}));
CFRunLoopRun();
}
Ok(())
}

type DynFn = dyn Fn() + 'static;
pub static mut STOP_LOOP: Option<Box<DynFn>> = None;
1 change: 1 addition & 0 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pub use crate::macos::display::display_size;
pub use crate::macos::grab::grab;
pub use crate::macos::keyboard::Keyboard;
pub use crate::macos::listen::listen;
pub use crate::macos::listen::stop_listen;
pub use crate::macos::simulate::simulate;
30 changes: 27 additions & 3 deletions src/windows/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use crate::rdev::{Event, EventType, ListenError};
use crate::windows::common::{convert, set_key_hook, set_mouse_hook, HookError, HOOK, KEYBOARD};
use std::os::raw::c_int;
use std::ptr::null_mut;
use std::sync::mpsc;
use std::thread;
use std::time::SystemTime;
use winapi::shared::minwindef::{LPARAM, LRESULT, WPARAM};
use winapi::um::winuser::{CallNextHookEx, GetMessageA, HC_ACTION};
use winapi::um::winuser::{CallNextHookEx, PeekMessageA, HC_ACTION};

static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event)>> = None;

Expand Down Expand Up @@ -41,6 +43,14 @@ unsafe extern "system" fn raw_callback(code: c_int, param: WPARAM, lpdata: LPARA
CallNextHookEx(HOOK, code, param, lpdata)
}

pub fn stop_listen() {
unsafe {
if let Some(stop_loop) = STOP_LOOP.as_ref() {
stop_loop();
}
}
}

pub fn listen<T>(callback: T) -> Result<(), ListenError>
where
T: FnMut(Event) + 'static,
Expand All @@ -49,8 +59,22 @@ where
GLOBAL_CALLBACK = Some(Box::new(callback));
set_key_hook(raw_callback)?;
set_mouse_hook(raw_callback)?;

GetMessageA(null_mut(), null_mut(), 0, 0);
let (sender, receiver) = mpsc::channel();
STOP_LOOP = Some(Box::new(move || {
sender.send(true).unwrap();
}));
loop {
if let Ok(stop_listen) = receiver.try_recv() {
if stop_listen {
println!("stop loop successed");
break;
}
}
PeekMessageA(null_mut(), null_mut(), 0, 0, 0);
}
}
Ok(())
}

type DynFn = dyn Fn() + 'static;
pub static mut STOP_LOOP: Option<Box<DynFn>> = None;
1 change: 1 addition & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub use crate::windows::display::display_size;
pub use crate::windows::grab::grab;
pub use crate::windows::keyboard::Keyboard;
pub use crate::windows::listen::listen;
pub use crate::windows::listen::stop_listen;
pub use crate::windows::simulate::simulate;