fix(macos): forward back/forward mouse buttons in capture and emulation (#392)

* fix(macos): forward back/forward mouse buttons in capture and emulation

OtherMouseDown/Up events on macOS carry a button number field that
distinguishes middle (2), back (3), and forward (4) buttons. The
capture backend was unconditionally mapping all OtherMouse events to
BTN_MIDDLE, silently dropping back/forward. The emulation backend had
no match arms for BTN_BACK/BTN_FORWARD, causing them to be dropped
with a warning.

Fix capture by reading MOUSE_EVENT_BUTTON_NUMBER and mapping 3->BTN_BACK,
4->BTN_FORWARD. Fix emulation by adding match arms for BTN_BACK/BTN_FORWARD
and setting MOUSE_EVENT_BUTTON_NUMBER on the emitted CGEvent so macOS
apps receive the correct button identity.

* fix(macos): track button state and double-clicks by evdev code instead of CGMouseButton

Back, forward, and middle buttons all map to CGMouseButton::Center on
macOS, which caused them to share a single pressed-state boolean and
alias in double-click detection. Replace the ButtonState struct with a
HashSet<u32> keyed by evdev button code so each button is tracked
independently.

---------

Co-authored-by: Ferdinand Schober <ferdinandschober20@gmail.com>
This commit is contained in:
Ty Smith
2026-02-22 08:45:53 -08:00
committed by GitHub
parent bcf9c35301
commit 27225ed564
2 changed files with 73 additions and 69 deletions

View File

@@ -18,7 +18,9 @@ use core_graphics::{
event_source::{CGEventSource, CGEventSourceStateID},
};
use futures_core::Stream;
use input_event::{BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent};
use input_event::{
BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent,
};
use keycode::{KeyMap, KeyMapping};
use libc::c_void;
use once_cell::unsync::Lazy;
@@ -304,16 +306,28 @@ fn get_events(
})))
}
CGEventType::OtherMouseDown => {
let btn_num = ev.get_integer_value_field(EventField::MOUSE_EVENT_BUTTON_NUMBER);
let button = match btn_num {
3 => BTN_BACK,
4 => BTN_FORWARD,
_ => BTN_MIDDLE,
};
result.push(CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
time: 0,
button: BTN_MIDDLE,
button,
state: 1,
})))
}
CGEventType::OtherMouseUp => {
let btn_num = ev.get_integer_value_field(EventField::MOUSE_EVENT_BUTTON_NUMBER);
let button = match btn_num {
3 => BTN_BACK,
4 => BTN_FORWARD,
_ => BTN_MIDDLE,
};
result.push(CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
time: 0,
button: BTN_MIDDLE,
button,
state: 0,
})))
}