Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ pub use crate::codes_conv::*;
pub use keycodes::android::{
code_from_key as android_keycode_from_key, key_from_code as android_key_from_code,
};
pub use keycodes::chrome::{
code_from_key as chrome_keycode_from_key, key_from_code as chrome_key_from_code,
};
pub use keycodes::linux::{
code_from_key as linux_keycode_from_key, key_from_code as linux_key_from_code,
};
Expand All @@ -252,13 +255,12 @@ pub use keycodes::windows::{
get_win_key, key_from_code as win_key_from_keycode, key_from_scancode as win_key_from_scancode,
scancode_from_key as win_scancode_from_key,
};
pub use keycodes::chrome::{
code_from_key as chrome_keycode_from_key, key_from_code as chrome_key_from_code,
};

#[cfg(target_os = "macos")]
pub use crate::keycodes::macos::{code_from_key, key_from_code, virtual_keycodes::*};
#[cfg(target_os = "macos")]
pub use crate::macos::exit_listen;
#[cfg(target_os = "macos")]
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
#[cfg(target_os = "macos")]
pub use crate::macos::{set_is_main_thread, Keyboard, VirtualInput};
Expand Down
16 changes: 14 additions & 2 deletions src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ unsafe extern "C" fn raw_callback(
cg_event
}

static mut CUR_LOOP: CFRunLoopSourceRef = std::ptr::null_mut();

pub fn listen<T>(callback: T) -> Result<(), ListenError>
where
T: FnMut(Event) + 'static,
Expand Down Expand Up @@ -59,11 +61,21 @@ where
return Err(ListenError::LoopSourceError);
}

let current_loop = CFRunLoopGetMain();
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);
CUR_LOOP = CFRunLoopGetCurrent() as _;
CFRunLoopAddSource(CUR_LOOP, _loop, kCFRunLoopCommonModes);

CGEventTapEnable(tap, true);
CFRunLoopRun();
}
Ok(())
}

pub fn exit_listen() -> Result<(), ListenError> {
unsafe {
if !CUR_LOOP.is_null() {
CFRunLoopStop(CUR_LOOP);
CUR_LOOP = std::ptr::null_mut();
}
}
Ok(())
}
2 changes: 1 addition & 1 deletion src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use crate::macos::common::{map_keycode, set_is_main_thread};
pub use crate::macos::display::display_size;
pub use crate::macos::grab::{exit_grab, grab, is_grabbed};
pub use crate::macos::keyboard::Keyboard;
pub use crate::macos::listen::listen;
pub use crate::macos::listen::{exit_listen, listen};
pub use crate::macos::simulate::{
set_keyboard_extra_info, set_mouse_extra_info, simulate, VirtualInput,
};