Skip to content

Commit

Permalink
macOS: Simulated input is independent of the physical keyboard state
Browse files Browse the repository at this point in the history
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 #297
  • Loading branch information
pentamassiv committed Sep 10, 2024
1 parent 5bf5be2 commit f807f41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ impl Enigo {
release_keys_when_dropped,
event_source_user_data,
open_prompt_to_get_permissions,
independent_of_keyboard_state,
..
} = settings;

Expand All @@ -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"));
};

Expand Down

0 comments on commit f807f41

Please sign in to comment.