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

Add drag event on macos #140

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
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
12 changes: 10 additions & 2 deletions examples/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ fn main() {

send(&EventType::MouseMove { x: 0.0, y: 0.0 });
send(&EventType::MouseMove { x: 400.0, y: 400.0 });
send(&EventType::ButtonPress(Button::Left));
send(&EventType::ButtonRelease(Button::Right));
send(&EventType::ButtonPress {
button: Button::Left,
x: None,
y: None,
});
send(&EventType::ButtonRelease {
button: Button::Right,
x: None,
y: None,
});
send(&EventType::Wheel {
delta_x: 0,
delta_y: 1,
Expand Down
33 changes: 31 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ mod macos;
#[cfg(target_os = "macos")]
pub use crate::macos::Keyboard;
#[cfg(target_os = "macos")]
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
use crate::macos::{
display_size as _display_size, get_current_mouse_location as _get_current_mouse_location,
listen as _listen, simulate as _simulate, stop_listen as _stop_listen,
};

#[cfg(target_os = "linux")]
mod linux;
Expand All @@ -242,7 +245,10 @@ mod windows;
#[cfg(target_os = "windows")]
pub use crate::windows::Keyboard;
#[cfg(target_os = "windows")]
use crate::windows::{display_size as _display_size, listen as _listen, simulate as _simulate};
use crate::windows::{
display_size as _display_size, get_current_mouse_location as _get_current_mouse_location,
listen as _listen, simulate as _simulate, stop_listen as _stop_listen,
};

/// Listening to global events. Caveat: On MacOS, you require the listen
/// loop needs to be the primary app (no fork before) and need to have accessibility
Expand Down Expand Up @@ -272,6 +278,29 @@ where
_listen(callback)
}

pub fn stop_listen() {
_stop_listen();
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
pub x: f64,
pub y: f64,
}

pub fn get_current_mouse_location() -> Option<Point> {
unsafe {
if let Some(point) = _get_current_mouse_location() {
Some(Point {
x: point.x as f64,
y: point.y as f64,
})
} else {
None
}
}
}

/// Sending some events
///
/// ```no_run
Expand Down
46 changes: 34 additions & 12 deletions src/macos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::macos::keyboard::Keyboard;
use crate::rdev::{Button, Event, EventType};
use cocoa::base::id;
use core_graphics::event::{CGEvent, CGEventFlags, CGEventTapLocation, CGEventType, EventField};
use core_graphics::geometry::CGPoint;
use lazy_static::lazy_static;
use std::convert::TryInto;
use std::os::raw::c_void;
Expand Down Expand Up @@ -75,7 +76,7 @@ extern "C" {
pub fn CFRunLoopGetCurrent() -> CFRunLoopRef;
pub fn CGEventTapEnable(tap: CFMachPortRef, enable: bool);
pub fn CFRunLoopRun();

pub fn CFRunLoopStop(rl: CFRunLoopRef);
pub static kCFRunLoopCommonModes: CFRunLoopMode;

}
Expand All @@ -96,18 +97,29 @@ pub unsafe fn convert(
cg_event: &CGEvent,
keyboard_state: &mut Keyboard,
) -> Option<Event> {
let CGPoint { x, y } = cg_event.location();
let option_type = match _type {
CGEventType::LeftMouseDown => Some(EventType::ButtonPress(Button::Left)),
CGEventType::LeftMouseUp => Some(EventType::ButtonRelease(Button::Left)),
CGEventType::RightMouseDown => Some(EventType::ButtonPress(Button::Right)),
CGEventType::RightMouseUp => Some(EventType::ButtonRelease(Button::Right)),
CGEventType::MouseMoved => {
let point = cg_event.location();
Some(EventType::MouseMove {
x: point.x,
y: point.y,
})
}
CGEventType::LeftMouseDown => Some(EventType::ButtonPress {
button: Button::Left,
x,
y,
}),
CGEventType::LeftMouseUp => Some(EventType::ButtonRelease {
button: Button::Left,
x,
y,
}),
CGEventType::RightMouseDown => Some(EventType::ButtonPress {
button: Button::Right,
x,
y,
}),
CGEventType::RightMouseUp => Some(EventType::ButtonRelease {
button: Button::Right,
x,
y,
}),
CGEventType::MouseMoved => Some(EventType::MouseMove { x, y }),
CGEventType::KeyDown => {
let code = cg_event.get_integer_value_field(EventField::KEYBOARD_EVENT_KEYCODE);
Some(EventType::KeyPress(key_from_code(code.try_into().ok()?)))
Expand Down Expand Up @@ -135,6 +147,16 @@ pub unsafe fn convert(
cg_event.get_integer_value_field(EventField::SCROLL_WHEEL_EVENT_POINT_DELTA_AXIS_2);
Some(EventType::Wheel { delta_x, delta_y })
}
CGEventType::LeftMouseDragged => Some(EventType::Drag {
button: Button::Left,
x,
y,
}),
CGEventType::RightMouseDragged => Some(EventType::Drag {
button: Button::Right,
x,
y,
}),
_ => None,
};
if let Some(event_type) = option_type {
Expand Down
17 changes: 16 additions & 1 deletion src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ unsafe extern "C" fn raw_callback(
let opt = KEYBOARD_STATE.lock();
if let Ok(mut keyboard) = opt {
if let Some(event) = convert(_type, &cg_event, &mut keyboard) {
if let Some(callback) = &mut GLOBAL_CALLBACK {
if let Some(ref mut callback) = GLOBAL_CALLBACK {
callback(event);
}
}
Expand Down Expand Up @@ -59,7 +59,22 @@ where
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);

CGEventTapEnable(tap, true);
STOP_LOOP = Some(Box::new(move || {
CFRunLoopStop(current_loop);
}));
CFRunLoopRun();
}
Ok(())
}

pub fn stop_listen() {
unsafe {
if let Some(stop_loop) = STOP_LOOP.as_ref() {
stop_loop();
STOP_LOOP = None;
}
}
}

type DynFn = dyn Fn() + 'static;
pub static mut STOP_LOOP: Option<Box<DynFn>> = None;
2 changes: 2 additions & 0 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub use crate::macos::display::display_size;
pub use crate::macos::grab::grab;
pub use crate::macos::keyboard::Keyboard;
pub use crate::macos::listen::listen;
pub use crate::macos::listen::stop_listen;
pub use crate::macos::simulate::get_current_mouse_location;
pub use crate::macos::simulate::simulate;
36 changes: 30 additions & 6 deletions src/macos/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ unsafe fn convert_native_with_source(
let code = code_from_key(*key)?;
CGEvent::new_keyboard_event(source, code, false).ok()
}
EventType::ButtonPress(button) => {
let point = get_current_mouse_location()?;
EventType::ButtonPress { button, x, y } => {
let point = CGPoint { x: *x, y: *y };
let event = match button {
Button::Left => CGEventType::LeftMouseDown,
Button::Right => CGEventType::RightMouseDown,
Expand All @@ -36,8 +36,8 @@ unsafe fn convert_native_with_source(
)
.ok()
}
EventType::ButtonRelease(button) => {
let point = get_current_mouse_location()?;
EventType::ButtonRelease { button, x, y } => {
let point = CGPoint { x: *x, y: *y };
let event = match button {
Button::Left => CGEventType::LeftMouseUp,
Button::Right => CGEventType::RightMouseUp,
Expand All @@ -52,7 +52,7 @@ unsafe fn convert_native_with_source(
.ok()
}
EventType::MouseMove { x, y } => {
let point = CGPoint { x: (*x), y: (*y) };
let point = CGPoint { x: *x, y: *y };
CGEvent::new_mouse_event(source, CGEventType::MouseMoved, point, CGMouseButton::Left)
.ok()
}
Expand All @@ -68,6 +68,27 @@ unsafe fn convert_native_with_source(
)
.ok()
}
EventType::Drag { button, x, y } => {
let point = CGPoint { x: *x, y: *y };
match button {
Button::Left => {
let mouse_type = CGEventType::LeftMouseDragged;
CGEvent::new_mouse_event(source, mouse_type, point, CGMouseButton::Left).ok()
}
Button::Right => {
let mouse_type = CGEventType::RightMouseDragged;
CGEvent::new_mouse_event(source, mouse_type, point, CGMouseButton::Right).ok()
}
Button::Middle => {
let mouse_type = CGEventType::OtherMouseDragged;
CGEvent::new_mouse_event(source, mouse_type, point, CGMouseButton::Center).ok()
}
Button::Unknown(_) => {
let mouse_type = CGEventType::OtherMouseDragged;
CGEvent::new_mouse_event(source, mouse_type, point, CGMouseButton::Center).ok()
}
}
}
}
}

Expand All @@ -76,7 +97,10 @@ unsafe fn convert_native(event_type: &EventType) -> Option<CGEvent> {
convert_native_with_source(event_type, source)
}

unsafe fn get_current_mouse_location() -> Option<CGPoint> {
///cause all of button events contain coordinate which can be used when they be simulated.
///so you don't need this fn when button press/release anymore.
#[allow(dead_code)]
pub unsafe fn get_current_mouse_location() -> Option<CGPoint> {
let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState).ok()?;
let event = CGEvent::new(source).ok()?;
Some(event.location())
Expand Down
17 changes: 15 additions & 2 deletions src/rdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,21 @@ pub enum EventType {
KeyPress(Key),
KeyRelease(Key),
/// Mouse Button
ButtonPress(Button),
ButtonRelease(Button),
ButtonPress {
button: Button,
x: f64,
y: f64,
},
ButtonRelease {
button: Button,
x: f64,
y: f64,
},
Drag {
button: Button,
x: f64,
y: f64,
},
/// Values in pixels. `EventType::MouseMove{x: 0, y: 0}` corresponds to the
/// top left corner, with x increasing downward and y increasing rightward
MouseMove {
Expand Down
60 changes: 46 additions & 14 deletions src/windows/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub unsafe fn get_button_code(lpdata: LPARAM) -> WORD {
}

pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<EventType> {
let (x, y) = get_point(lpdata);
let x = x as f64;
let y = y as f64;
match param.try_into() {
Ok(WM_KEYDOWN) | Ok(WM_SYSKEYDOWN) => {
let code = get_code(lpdata);
Expand All @@ -60,27 +63,56 @@ pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<EventType> {
let key = key_from_code(code as u16);
Some(EventType::KeyRelease(key))
}
Ok(WM_LBUTTONDOWN) => Some(EventType::ButtonPress(Button::Left)),
Ok(WM_LBUTTONUP) => Some(EventType::ButtonRelease(Button::Left)),
Ok(WM_MBUTTONDOWN) => Some(EventType::ButtonPress(Button::Middle)),
Ok(WM_MBUTTONUP) => Some(EventType::ButtonRelease(Button::Middle)),
Ok(WM_RBUTTONDOWN) => Some(EventType::ButtonPress(Button::Right)),
Ok(WM_RBUTTONUP) => Some(EventType::ButtonRelease(Button::Right)),
Ok(WM_LBUTTONDOWN) => Some(EventType::ButtonPress {
button: Button::Left,
x,
y,
}),
Ok(WM_LBUTTONUP) => Some(EventType::ButtonRelease {
button: Button::Left,
x,
y,
}),
Ok(WM_MBUTTONDOWN) => Some(EventType::ButtonPress {
button: Button::Middle,
x,
y,
}),
Ok(WM_MBUTTONUP) => Some(EventType::ButtonRelease {
button: Button::Middle,
x,
y,
}),
Ok(WM_RBUTTONDOWN) => Some(EventType::ButtonPress {
button: Button::Right,
x,
y,
}),
Ok(WM_RBUTTONUP) => Some(EventType::ButtonRelease {
button: Button::Right,
x,
y,
}),
Ok(WM_XBUTTONDOWN) => {
let code = get_button_code(lpdata) as u8;
Some(EventType::ButtonPress(Button::Unknown(code)))
Some(EventType::ButtonPress {
button: Button::Unknown(code),
x,
y,
})
}
Ok(WM_XBUTTONUP) => {
let code = get_button_code(lpdata) as u8;
Some(EventType::ButtonRelease(Button::Unknown(code)))
}
Ok(WM_MOUSEMOVE) => {
let (x, y) = get_point(lpdata);
Some(EventType::MouseMove {
x: x as f64,
y: y as f64,
Some(EventType::ButtonRelease {
button: Button::Unknown(code),
x,
y,
})
}
Ok(WM_MOUSEMOVE) => Some(EventType::MouseMove {
x: x as f64,
y: y as f64,
}),
Ok(WM_MOUSEWHEEL) => {
let delta = get_delta(lpdata) as c_short;
Some(EventType::Wheel {
Expand Down
Loading