Skip to content

Commit e43c6a2

Browse files
committed
feat: add stop_listen for mac
1 parent cb9a29e commit e43c6a2

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

examples/simply_listen.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use rdev::{listen, Event, EventType};
2+
3+
fn callback(event: Event) {
4+
match event.event_type {
5+
EventType::KeyPress(_key) | EventType::KeyRelease(_key) => {
6+
println!("User wrote {:?}", event.unicode);
7+
}
8+
_ => (),
9+
}
10+
}
11+
12+
fn main() {
13+
// This will block.
14+
use std::thread;
15+
use std::time::Duration;
16+
let handle = thread::spawn(|| {
17+
if let Err(error) = listen(callback) {
18+
println!("Error: {:?}", error)
19+
}
20+
});
21+
thread::sleep(Duration::from_secs(5));
22+
rdev::stop_listen().unwrap();
23+
let _ = handle.join();
24+
println!("Done");
25+
}

src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ pub use crate::codes_conv::*;
238238
pub use keycodes::android::{
239239
code_from_key as android_keycode_from_key, key_from_code as android_key_from_code,
240240
};
241+
pub use keycodes::chrome::{
242+
code_from_key as chrome_keycode_from_key, key_from_code as chrome_key_from_code,
243+
};
241244
pub use keycodes::linux::{
242245
code_from_key as linux_keycode_from_key, key_from_code as linux_key_from_code,
243246
};
@@ -252,14 +255,14 @@ pub use keycodes::windows::{
252255
get_win_key, key_from_code as win_key_from_keycode, key_from_scancode as win_key_from_scancode,
253256
scancode_from_key as win_scancode_from_key,
254257
};
255-
pub use keycodes::chrome::{
256-
code_from_key as chrome_keycode_from_key, key_from_code as chrome_key_from_code,
257-
};
258258

259259
#[cfg(target_os = "macos")]
260260
pub use crate::keycodes::macos::{code_from_key, key_from_code, virtual_keycodes::*};
261261
#[cfg(target_os = "macos")]
262-
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
262+
use crate::macos::{
263+
display_size as _display_size, listen as _listen, simulate as _simulate,
264+
stop_listen as _stop_listen,
265+
};
263266
#[cfg(target_os = "macos")]
264267
pub use crate::macos::{set_is_main_thread, Keyboard, VirtualInput};
265268
#[cfg(target_os = "macos")]
@@ -312,6 +315,11 @@ where
312315
_listen(callback)
313316
}
314317

318+
#[cfg(target_os = "macos")]
319+
pub fn stop_listen() -> Result<(), ListenError> {
320+
_stop_listen()
321+
}
322+
315323
/// Sending some events
316324
///
317325
/// ```no_run

src/macos/listen.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ unsafe extern "C" fn raw_callback(
3030
cg_event
3131
}
3232

33+
static mut CUR_LOOP: CFRunLoopSourceRef = std::ptr::null_mut();
34+
3335
pub fn listen<T>(callback: T) -> Result<(), ListenError>
3436
where
3537
T: FnMut(Event) + 'static,
@@ -59,11 +61,23 @@ where
5961
return Err(ListenError::LoopSourceError);
6062
}
6163

62-
let current_loop = CFRunLoopGetMain();
63-
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);
64+
CUR_LOOP = CFRunLoopGetCurrent() as _;
65+
CFRunLoopAddSource(CUR_LOOP, _loop, kCFRunLoopCommonModes);
66+
// let current_loop = CFRunLoopGetMain();
67+
// CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);
6468

6569
CGEventTapEnable(tap, true);
6670
CFRunLoopRun();
6771
}
6872
Ok(())
6973
}
74+
75+
pub fn stop_listen() -> Result<(), ListenError> {
76+
unsafe {
77+
if !CUR_LOOP.is_null() {
78+
CFRunLoopStop(CUR_LOOP);
79+
CUR_LOOP = std::ptr::null_mut();
80+
}
81+
}
82+
Ok(())
83+
}

src/macos/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use crate::macos::common::{map_keycode, set_is_main_thread};
99
pub use crate::macos::display::display_size;
1010
pub use crate::macos::grab::{exit_grab, grab, is_grabbed};
1111
pub use crate::macos::keyboard::Keyboard;
12-
pub use crate::macos::listen::listen;
12+
pub use crate::macos::listen::{listen, stop_listen};
1313
pub use crate::macos::simulate::{
1414
set_keyboard_extra_info, set_mouse_extra_info, simulate, VirtualInput,
1515
};

0 commit comments

Comments
 (0)