Skip to content

Commit

Permalink
win: Added helper function to tell Windows to respect the users scali…
Browse files Browse the repository at this point in the history
…ng factor

Fixes #370
  • Loading branch information
pentamassiv committed Dec 18, 2024
1 parent d13c6af commit 97d3b29
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Added
- macOS: Add to support Mouse special key(Back, Forward)
- win: Helper function to tell Windows to respect the users scaling settings `set_dpi_awareness`. Read the docs before using it

## Removed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ windows = { version = "0.58", features = [
"Win32_UI_TextServices",
"Win32_UI_WindowsAndMessaging",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_HiDpi",
] }

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
7 changes: 7 additions & 0 deletions examples/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ use std::time::Duration;

fn main() {
env_logger::try_init().ok();

#[cfg(target_os = "windows")]
// This is needed on Windows if you want the application to respect the users scaling settings.
// Please look at the documentation of the function to see better ways to achive this and
// important gotchas
enigo::set_dpi_awareness().unwrap();

let wait_time = Duration::from_secs(2);
let mut enigo = Enigo::new(&Settings::default()).unwrap();

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub mod agent;
mod platform;
pub use platform::Enigo;

#[cfg(target_os = "windows")]
pub use platform::set_dpi_awareness;
#[cfg(target_os = "windows")]
pub use platform::EXT;

Expand Down
2 changes: 1 addition & 1 deletion src/win/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod win_impl;
pub use win_impl::{Enigo, EXT};
pub use win_impl::{set_dpi_awareness, Enigo, EXT};
25 changes: 25 additions & 0 deletions src/win/win_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,31 @@ impl Enigo {
}
}

/// Sets the current process to a specified dots per inch (dpi) awareness
/// context [see official documentation](https://learn.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-setprocessdpiawareness)
/// If you want your applications to respect the users scaling, you need to set
/// this. Otherwise the mouse coordinates and screen dimensions will be off.
///
/// It is recommended that you set the process-default DPI awareness via
/// application manifest, not an API call. See [Setting the default DPI awareness for a process](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiawarenesscontext) for more information. Setting the process-default DPI
/// awareness via API call can lead to unexpected application behavior.
/// It also needs to be set before any APIs are used that depend on the DPI and
/// before a UI is created.
/// Enigo is a library and should not set this, because
/// it will lead to unexpected scaling of the application. Only use it for
/// examples or if you know about the consequences
///
/// # Errors
/// An error is thrown if the default API awareness mode for the process has
/// already been set (via a previous API call or within the application
/// manifest)
pub fn set_dpi_awareness() -> Result<(), ()> {
use windows::Win32::UI::HiDpi::SetProcessDpiAwareness;
use windows::Win32::UI::HiDpi::PROCESS_PER_MONITOR_DPI_AWARE;

unsafe { SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) }.map_err(|_| ())
}

impl Drop for Enigo {
// Release the held keys before the connection is dropped
fn drop(&mut self) {
Expand Down

0 comments on commit 97d3b29

Please sign in to comment.