Skip to content
Open
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
34 changes: 34 additions & 0 deletions src/macos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ pub fn set_is_main_thread(b: bool) {
KEYBOARD_STATE.lock().unwrap().set_is_main_thread(b);
}

fn button_from_macos_button_number(code: u8) -> Button {
match code {
0 => Button::Left,
1 => Button::Right,
2 => Button::Middle,
code => Button::Unknown(code),
}
}

pub unsafe fn convert(
_type: CGEventType,
cg_event: NonNull<CGEvent>,
Expand All @@ -32,6 +41,24 @@ pub unsafe fn convert(
CGEventType::LeftMouseUp => Some(EventType::ButtonRelease(Button::Left)),
CGEventType::RightMouseDown => Some(EventType::ButtonPress(Button::Right)),
CGEventType::RightMouseUp => Some(EventType::ButtonRelease(Button::Right)),
CGEventType::OtherMouseDown => {
let code = CGEvent::integer_value_field(
Some(cg_event.as_ref()),
CGEventField::MouseEventButtonNumber,
);
Some(EventType::ButtonPress(button_from_macos_button_number(
code.try_into().ok()?,
)))
}
CGEventType::OtherMouseUp => {
let code = CGEvent::integer_value_field(
Some(cg_event.as_ref()),
CGEventField::MouseEventButtonNumber,
);
Some(EventType::ButtonRelease(button_from_macos_button_number(
code.try_into().ok()?,
)))
}
CGEventType::MouseMoved => {
let point = CGEvent::location(Some(cg_event.as_ref()));
// let point = cg_event.location();
Expand All @@ -54,6 +81,13 @@ pub unsafe fn convert(
y: point.y,
})
}
CGEventType::OtherMouseDragged => {
let point = CGEvent::location(Some(cg_event.as_ref()));
Some(EventType::MouseMove {
x: point.x,
y: point.y,
})
}
CGEventType::KeyDown => {
// println!("Received {cg_event:?}");
let code = CGEvent::integer_value_field(
Expand Down
Loading