From da1a1f75487d53b8a35735d7b90998bd0e93c7ff Mon Sep 17 00:00:00 2001 From: pentamassiv <91755244+pentamassiv@users.noreply.github.com> Date: Wed, 11 Sep 2024 03:32:04 +0200 Subject: [PATCH] macOS: Remove all sleeps and add one when the `Enigo` struct is dropped There were two reasons why we needed to sleep on macOS. The OS needed "some time" to apply a modifier and correctly set the CGEventFlags. We can remove the sleep and manually set the flags. Another reason for the sleeps is that all pending events get ignored when the program terminates. The sleeps were needed so that the OS had enough time to handle all events before the Enigo struct gets dropped. Sleeps during the execution are much more annoying then at the end of the program, so there is now a sleep for two seconds when the Enigo struct gets dropped. I just assumed that to be enough for most cases. I did not find a way to ask the OS if all events were handled. We could probably do better here and calculate a duration based on the time when the last event was sent and how many were sent. Other things that were fixed: - Thanks to the sleep in the Drop trait of Enigo, moving the mouse also works if it is the only function that gets executed. It used to finish too fast and we didn't have a sleep so it got ignored because Enigo was dropped before it was handled by the OS - By manually setting the EventFlags, CapsLock now also works. Before this commit, sending text would fail if a modifier was held and the text was too small to be sent as one chunk. --- CHANGES.md | 6 +- src/lib.rs | 3 - src/macos/macos_impl.rs | 270 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 253 insertions(+), 26 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b8a0c712..894532d5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,21 +4,25 @@ - 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`) - macOS: Do not coalesce mouse events to allow precise control - win: Fallback to entering a `Key::Unicode` as unicode if there is no key with that character +- macOS: Removed setting and functions related to the delay on macOS because a sleep is no longer necessary ## Added - all: `Key::PrintScr` - all: There finally are some tests in the CI to increase the development speed and prevent regressions - win, macOS: Allow marking events that were created by enigo. Have a look at the additional field of the `Settings` struct and the new method `get_marker_value` of the `Enigo` struct (only available on Windows and macOS) - macOS: Fallback to ASCII-capable keyboard layout for handling non-standard input sources -- macOS: Check if the application has the neccessary permissions. If they are missing, `enigo` will ask the user to grant them. You can change this default behavior with the `Settings` when constructing an `Enigo` struct. +- macOS: Check if the application has the necessary permissions. If they are missing, `enigo` will ask the user to grant them. You can change this default behavior with the `Settings` when constructing an `Enigo` struct. - all: Added `Token::Location` and `Token::MainDisplay` mostly for debugging purposes. They allow you to check if your expectations are correct ## Fixed +- macOS: No more sleeps!! (Only when the `Enigo` struct is dropped) ([#105](https://github.com/enigo-rs/enigo/issues/105)) - win: Respect the language of the current window to determine which scancodes to send - win: Send the virtual key and its scan code in the events to work with programs that only look at one of the two - macOS: Moving the mouse no longer breaks simulating input - win: Fix being unable to enter text containing multiple newline chars - macOS: Switched keycodes of `Key::Launchpad` and `Key::MissionControl` +- macOS: `CapsLock` works ([#163](https://github.com/enigo-rs/enigo/issues/163)) +- macOS: Moving the mouse works also if it is the only function ([#182](https://github.com/enigo-rs/enigo/issues/182)) # 0.2.1 ## Changed diff --git a/src/lib.rs b/src/lib.rs index 39790941..03135e27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -436,8 +436,6 @@ impl Error for NewConError {} #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Settings { - /// Sleep delay on macOS - pub mac_delay: u32, /// Sleep delay on Linux X11 pub linux_delay: u32, /// Display name to connect to when using Linux X11 @@ -468,7 +466,6 @@ impl Default for Settings { fn default() -> Self { debug!("using default settings"); Self { - mac_delay: 20, linux_delay: 12, x11_display: None, wayland_display: None, diff --git a/src/macos/macos_impl.rs b/src/macos/macos_impl.rs index b15513aa..a814fee1 100644 --- a/src/macos/macos_impl.rs +++ b/src/macos/macos_impl.rs @@ -72,12 +72,12 @@ extern "C" { /// The main struct for handling the event emitting pub struct Enigo { - delay: u64, event_source: CGEventSource, display: CGDisplay, held: (Vec, Vec), // Currently held keys event_source_user_data: i64, release_keys_when_dropped: bool, + event_flags: CGEventFlags, double_click_delay: Duration, // TODO: Use mem::variant_count::