Skip to content

Commit

Permalink
macOS: Remove all sleeps and add one when the Enigo struct is dropped
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pentamassiv committed Oct 27, 2024
1 parent e06beb3 commit da1a1f7
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 26 deletions.
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit da1a1f7

Please sign in to comment.