From f807f4129a16812b6e1da0a0f46331db549d0333 Mon Sep 17 00:00:00 2001 From: pentamassiv <91755244+pentamassiv@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:56:00 +0200 Subject: [PATCH] macOS: Simulated input is independent of the physical keyboard state Previously the state of the physical keyboard effected the result of the simulated input. If the Command key was held on the physical keyboard, the text() function no longer worked. By default the two are now independent, but this behavior can be changed with the Settings struct (`independent_of_keyboard_state`). Fixes https://github.com/enigo-rs/enigo/issues/297 --- CHANGES.md | 1 + src/lib.rs | 6 ++++++ src/macos/macos_impl.rs | 9 +++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 46092d8e..db141847 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Unreleased ## Changed - all: The keys `Print` and `Snapshot` were deprecated because `Print` had the wrong virtual key associated with it on Windows. Use `Key::PrintScr` instead +- macOS: The simulated input is no longer effected by the state of the physical keyboard. This default behavior can be changed via the Settings (`independent_of_keyboard_state`) ## Added - all: `Key::PrintScr` diff --git a/src/lib.rs b/src/lib.rs index 34393729..7e3ea9c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -451,6 +451,11 @@ pub struct Settings { /// Open a prompt to ask the user for the permission to simulate input if /// they are missing. This only works on macOS. The default is true. pub open_prompt_to_get_permissions: bool, + /// The simulated input is independent from the pressed keys on the + /// physical keyboard. This only works on macOS. + /// The default is true. If the Shift key for example is pressed, + /// following simulated input will not be capitalized. + pub independent_of_keyboard_state: bool, } impl Default for Settings { @@ -465,6 +470,7 @@ impl Default for Settings { event_source_user_data: None, release_keys_when_dropped: true, open_prompt_to_get_permissions: true, + independent_of_keyboard_state: true, } } } diff --git a/src/macos/macos_impl.rs b/src/macos/macos_impl.rs index bdd3a442..f2ae5a5f 100644 --- a/src/macos/macos_impl.rs +++ b/src/macos/macos_impl.rs @@ -488,6 +488,7 @@ impl Enigo { release_keys_when_dropped, event_source_user_data, open_prompt_to_get_permissions, + independent_of_keyboard_state, .. } = settings; @@ -504,8 +505,12 @@ impl Enigo { // Returns the double click interval (https://developer.apple.com/documentation/appkit/nsevent/1528384-doubleclickinterval). This is a TimeInterval which is a f64 of the number of seconds let double_click_delay = double_click_delay.mul_f64(double_click_delay_setting); - let Ok(event_source) = CGEventSource::new(CGEventSourceStateID::CombinedSessionState) - else { + let event_source_state = if *independent_of_keyboard_state { + CGEventSourceStateID::Private + } else { + CGEventSourceStateID::CombinedSessionState + }; + let Ok(event_source) = CGEventSource::new(event_source_state) else { return Err(NewConError::EstablishCon("failed creating event source")); };