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 high resolution mouse to Windows #118

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 2 additions & 5 deletions examples/simulate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rdev::{simulate, Button, EventType, Key, SimulateError};
use rdev::{simulate, Button, EventType, Key, MouseScrollDelta, SimulateError};
use std::{thread, time};

fn send(event_type: &EventType) {
Expand All @@ -21,8 +21,5 @@ fn main() {
send(&EventType::MouseMove { x: 400.0, y: 400.0 });
send(&EventType::ButtonPress(Button::Left));
send(&EventType::ButtonRelease(Button::Right));
send(&EventType::Wheel {
delta_x: 0,
delta_y: 1,
});
send(&EventType::Wheel(MouseScrollDelta::LineDelta(0.0, 1.0)));
}
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! # Sending some events
//!
//! ```no_run
//! use rdev::{simulate, Button, EventType, Key, SimulateError};
//! use rdev::{simulate, Button, EventType, MouseScrollDelta, Key, SimulateError};
//! use std::{thread, time};
//!
//! fn send(event_type: &EventType) {
Expand All @@ -64,10 +64,7 @@
//! send(&EventType::MouseMove { x: 400.0, y: 400.0 });
//! send(&EventType::ButtonPress(Button::Left));
//! send(&EventType::ButtonRelease(Button::Right));
//! send(&EventType::Wheel {
//! delta_x: 0,
//! delta_y: 1,
//! });
//! send(&EventType::Wheel(MouseScrollDelta::LineDelta(0.0, 1.0)));
//! ```
//! # Main structs
//! ## Event
Expand Down Expand Up @@ -220,7 +217,7 @@
mod rdev;
pub use crate::rdev::{
Button, DisplayError, Event, EventType, GrabCallback, GrabError, Key, KeyboardState,
ListenError, SimulateError,
ListenError, MouseScrollDelta, SimulateError,
};

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -275,7 +272,7 @@ where
/// Sending some events
///
/// ```no_run
/// use rdev::{simulate, Button, EventType, Key, SimulateError};
/// use rdev::{simulate, Button, EventType,MouseScrollDelta, Key, SimulateError};
/// use std::{thread, time};
///
/// fn send(event_type: &EventType) {
Expand All @@ -298,10 +295,7 @@ where
/// send(&EventType::MouseMove { x: 400.0, y: 400.0 });
/// send(&EventType::ButtonPress(Button::Left));
/// send(&EventType::ButtonRelease(Button::Right));
/// send(&EventType::Wheel {
/// delta_x: 0,
/// delta_y: 1,
/// });
/// send(&EventType::Wheel(MouseScrollDelta::LineDelta(0.0, 1.0)));
/// }
/// ```
pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> {
Expand Down Expand Up @@ -357,11 +351,17 @@ pub use crate::windows::grab as _grab;
/// }
/// ```
#[cfg(feature = "unstable_grab")]
pub fn grab<T>(callback: T) -> Result<(), GrabError>
pub fn grab<T>(event_types: EventTypes, callback: T) -> Result<(), GrabError>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a useful optimisation on platforms like Windows.

where
T: Fn(Event) -> Option<Event> + 'static,
{
_grab(callback)
_grab(event_types, callback)
}

#[cfg(feature = "unstable_grab")]
pub struct EventTypes {
pub keyboard: bool,
pub mouse: bool,
}

#[cfg(test)]
Expand Down
12 changes: 8 additions & 4 deletions src/rdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,14 @@ pub enum EventType {
/// `delta_y` represents vertical scroll and `delta_x` represents horizontal scroll.
/// Positive values correspond to scrolling up or right and negative values
/// correspond to scrolling down or left
Wheel {
delta_x: i64,
delta_y: i64,
},
Wheel(MouseScrollDelta),
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum MouseScrollDelta {
LineDelta(f32, f32),
// Not supported yet PixelDelta(PhysicalPosition<f64>),
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This imitates the Winit API


/// When events arrive from the OS they get some additional information added from
Expand Down
18 changes: 9 additions & 9 deletions src/windows/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rdev::{Button, EventType};
use crate::rdev::{Button, EventType, MouseScrollDelta};
use crate::windows::keyboard::Keyboard;
use crate::windows::keycodes::key_from_code;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -83,17 +83,17 @@ pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<EventType> {
}
Ok(WM_MOUSEWHEEL) => {
let delta = get_delta(lpdata) as c_short;
Some(EventType::Wheel {
delta_x: 0,
delta_y: (delta / WHEEL_DELTA) as i64,
})
Some(EventType::Wheel(MouseScrollDelta::LineDelta(
0.0,
delta as f32 / WHEEL_DELTA as f32,
)))
}
Ok(WM_MOUSEHWHEEL) => {
let delta = get_delta(lpdata) as c_short;
Some(EventType::Wheel {
delta_x: (delta / WHEEL_DELTA) as i64,
delta_y: 0,
})
Some(EventType::Wheel(MouseScrollDelta::LineDelta(
0.0,
delta as f32 / WHEEL_DELTA as f32,
)))
}
_ => None,
}
Expand Down
12 changes: 8 additions & 4 deletions src/windows/grab.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::rdev::{Event, EventType, GrabError};
use crate::windows::common::{convert, set_key_hook, set_mouse_hook, HookError, HOOK, KEYBOARD};
use crate::EventTypes;
use std::ptr::null_mut;
use std::time::SystemTime;
use winapi::um::winuser::{CallNextHookEx, GetMessageA, HC_ACTION};
Expand Down Expand Up @@ -44,15 +45,18 @@ impl From<HookError> for GrabError {
}
}

pub fn grab<T>(callback: T) -> Result<(), GrabError>
pub fn grab<T>(event_types: EventTypes, callback: T) -> Result<(), GrabError>
where
T: FnMut(Event) -> Option<Event> + 'static,
{
unsafe {
GLOBAL_CALLBACK = Some(Box::new(callback));
set_key_hook(raw_callback)?;
set_mouse_hook(raw_callback)?;

if event_types.keyboard {
set_key_hook(raw_callback)?;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, the current keyboard hook can break things.
With my (very custom) keyboard, a few shortcuts do not work if I register this hook.

}
if event_types.mouse {
set_mouse_hook(raw_callback)?;
}
GetMessageA(null_mut(), null_mut(), 0, 0);
}
Ok(())
Expand Down
14 changes: 8 additions & 6 deletions src/windows/simulate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rdev::{Button, EventType, SimulateError};
use crate::rdev::{Button, EventType, MouseScrollDelta, SimulateError};
use crate::windows::keycodes::code_from_key;
use std::convert::TryFrom;
use std::mem::size_of;
Expand Down Expand Up @@ -95,20 +95,22 @@ pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> {
Button::Right => sim_mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0),
Button::Unknown(code) => sim_mouse_event(MOUSEEVENTF_XUP, (*code).into(), 0, 0),
},
EventType::Wheel { delta_x, delta_y } => {
if *delta_x != 0 {
EventType::Wheel(MouseScrollDelta::LineDelta(delta_x, delta_y)) => {
if *delta_x != 0.0 {
let delta_x = *delta_x * WHEEL_DELTA as f32;
sim_mouse_event(
MOUSEEVENTF_HWHEEL,
(c_short::try_from(*delta_x).map_err(|_| SimulateError)? * WHEEL_DELTA) as u32,
(c_short::try_from(delta_x as i16).map_err(|_| SimulateError)?) as u32,
0,
0,
)?;
}

if *delta_y != 0 {
if *delta_y != 0.0 {
let delta_y = *delta_y * WHEEL_DELTA as f32;
sim_mouse_event(
MOUSEEVENTF_WHEEL,
(c_short::try_from(*delta_y).map_err(|_| SimulateError)? * WHEEL_DELTA) as u32,
(c_short::try_from(delta_y as i16).map_err(|_| SimulateError)?) as u32,
0,
0,
)?;
Expand Down
11 changes: 9 additions & 2 deletions tests/grab.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lazy_static::lazy_static;
use rdev::{grab, listen, simulate, Event, EventType, Key};
use rdev::{grab, listen, simulate, Event, EventType, EventTypes, Key};
use serial_test::serial;
use std::error::Error;
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};
Expand Down Expand Up @@ -45,7 +45,14 @@ fn test_grab() -> Result<(), Box<dyn Error>> {
// Make sure grab ends up on top of listen so it can properly discard.
thread::sleep(Duration::from_secs(1));
let _grab = thread::spawn(move || {
grab(grab_tab).expect("Could not grab");
grab(
EventTypes {
keyboard: true,
mouse: true,
},
grab_tab,
)
.expect("Could not grab");
});

let recv = EVENT_CHANNEL.1.lock().expect("Failed to unlock Mutex");
Expand Down
12 changes: 3 additions & 9 deletions tests/listen_and_simulate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lazy_static::lazy_static;
use rdev::{listen, simulate, Button, Event, EventType, Key};
use rdev::{listen, simulate, Button, Event, EventType, Key, MouseScrollDelta};
use serial_test::serial;
use std::error::Error;
use std::iter::Iterator;
Expand Down Expand Up @@ -55,14 +55,8 @@ fn test_listen_and_simulate() -> Result<(), Box<dyn Error>> {
EventType::KeyRelease(Key::KeyS),
EventType::ButtonPress(Button::Right),
EventType::ButtonRelease(Button::Right),
EventType::Wheel {
delta_x: 0,
delta_y: 1,
},
EventType::Wheel {
delta_x: 0,
delta_y: -1,
},
EventType::Wheel(MouseScrollDelta::LineDelta(0.0, 1.0)),
EventType::Wheel(MouseScrollDelta::LineDelta(0.0, -1.0)),
]
.into_iter();
let click_events = (0..480).map(|pixel| EventType::MouseMove {
Expand Down