-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In macOS, mouse movement interrupts the press of the meta key. #201
Comments
Thank you for your report. I reproduced it successfully on the mac I use too. I look into it It also happens for me, if the raw keycode ( |
I also run into this issue, and agree that it would be nice to get rid of sleep. Unfortunately, Apple's Core Graphics documentation isn't very clear about the details/implements, which makes it challenging to understand its behavior. For this particular problem, the comment at #105 (comment) shares a workaround solution by using event flags in Core Graphics to set flag keys such as use core_graphics::{
event::{ CGEvent, CGEventTapLocation, KeyCode, CGEventFlags },
event_source::{ CGEventSource, CGEventSourceStateID },
};
// Define CG key code for "a" key
// Reference: https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h#L197
static A_KEY_CODE: u16 = 0x00;
fn click_cmd_a() {
// Implementation reference: https://stackoverflow.com/questions/2008126/cgeventpost-possible-bug-when-simulating-keyboard-events
// Event source state id reference: https://developer.apple.com/documentation/coregraphics/cgeventsourcestateid
let event_source_state_id = CGEventSourceStateID::CombinedSessionState;
let event_source = CGEventSource::new(event_source_state_id).unwrap();
// Event tap location reference: https://developer.apple.com/documentation/coregraphics/cgeventtaplocation
let event_tap_location = CGEventTapLocation::HID;
let press_cmd_a_event = CGEvent::new_keyboard_event(
event_source.clone(),
A_KEY_CODE,
true
).unwrap();
press_cmd_a_event.set_flags(CGEventFlags::CGEventFlagCommand); // Set flags to Cmd
press_cmd_a_event.post(event_tap_location);
let release_a_event = CGEvent::new_keyboard_event(
event_source.clone(),
A_KEY_CODE,
false
).unwrap();
release_a_event.set_flags(CGEventFlags::CGEventFlagNull); // Reset flags to null
release_a_event.post(event_tap_location);
// Release Cmd Key for completeness. May or may not be necessary
// given Apple's documentation is not clear on this.
let release_cmd_event = CGEvent::new_keyboard_event(
event_source.clone(),
KeyCode::COMMAND,
false
).unwrap();
release_cmd_event.post(event_tap_location);
} A potential future solution might be to leverage enigo.keys([Key::Command, Key::Unicode('a')], Click).unwrap(); This would likely require more thinkings to refine the solution and apis. |
Describe the bug
I use enigo to simulate pressing cmd+a on macOS to trigger the select all operation in the input box. It works fine when I don't move the mouse, but as soon as I move the mouse, only one 'a' character is outputted in the input box. It seems that moving the mouse interrupts the press of the cmd key.
To Reproduce
Area.mp4
Expected behavior
Trigger the cmd+a shortcut to select all text in the input box.
Environment (please complete the following information):
Additional context
None
The text was updated successfully, but these errors were encountered: