Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

livesplit-hotkey: Add Hook::new_consuming on macOS #755

Merged
merged 1 commit into from
Jan 20, 2024
Merged
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
23 changes: 17 additions & 6 deletions crates/livesplit-hotkey/src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use self::{
},
};
use crate::{ConsumePreference, Hotkey, KeyCode, Modifiers, Result};
use core::ptr::null_mut;
use std::{
collections::{hash_map::Entry, HashMap},
ffi::c_void,
Expand Down Expand Up @@ -97,9 +98,10 @@ impl Drop for Hook {

impl Hook {
pub fn new(consume: ConsumePreference) -> Result<Self> {
if matches!(consume, ConsumePreference::MustConsume) {
return Err(crate::Error::UnmatchedPreference);
}
let is_consuming = matches!(
consume,
ConsumePreference::PreferConsume | ConsumePreference::MustConsume,
);

let state = Arc::new(State {
hotkeys: Mutex::new(HashMap::new()),
Expand All @@ -124,7 +126,11 @@ impl Hook {
let port = CGEventTapCreate(
EventTapLocation::SESSION,
EventTapPlacement::HEAD_INSERT_EVENT_TAP,
EventTapOptions::DEFAULT_TAP,
if is_consuming {
EventTapOptions::DEFAULT_TAP
} else {
EventTapOptions::LISTEN_ONLY
},
EventMask::KEY_DOWN,
Some(callback),
state_ptr as *mut c_void,
Expand Down Expand Up @@ -479,7 +485,12 @@ unsafe extern "C" fn callback(
.get_mut(&key_code.with_modifiers(modifiers))
{
callback();
}

event
// If we handled the event and the hook is consuming, we should return
// null so the system deletes the event. If the hook is not consuming
// the return value will be ignored, so return null anyway.
null_mut()
} else {
event
}
}
Loading