Skip to content
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

macOS: Added workaround for entering text that starts with newline #261

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- linux: You can enter `Key::ScrollLock` now
- win: No more sleeps! Simulating input is done in 1 ms instead of 40+ ms. This is most obvious when entering long strings
- macOS: Added keys to control media, brightness, contrast, illumination and more
- macOS: Fix entering text that starts with newline characters

# 0.1.3

Expand Down
16 changes: 11 additions & 5 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ impl Keyboard for Enigo {
let mut indices = s.char_indices().map(|(idx, _)| idx).peekable();

std::iter::from_fn(move || {
let Some(start_idx) = indices.next() else {
return None;
};
let start_idx = indices.next()?;
for _ in 0..len - 1 {
indices.next();
}
Expand All @@ -324,15 +322,23 @@ impl Keyboard for Enigo {
}

debug!("\x1b[93mfast_text(text: {text})\x1b[0m");
// NOTE(dustin): This is a fix for issue https://github.com/enigo-rs/enigo/issues/68
// WORKAROUND: This is a fix for issue https://github.com/enigo-rs/enigo/issues/68
// The CGEventKeyboardSetUnicodeString function (used inside of
// event.set_string(chunk)) truncates strings down to 20 characters
for chunk in chunks(text, 20) {
for mut chunk in chunks(text, 20) {
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), 0, true) else {
return Err(InputError::Simulate(
"failed creating event to enter the text",
));
};
// WORKAROUND: This is a fix for issue https://github.com/enigo-rs/enigo/issues/260
// This is needed to get rid of all leading newline characters.
// event.set_string(chunk)) silently fails if the chunk starts with a newline
// character
while chunk.starts_with('\n') {
self.key(Key::Return, Direction::Click)?;
chunk = &chunk[1..];
}
event.set_string(chunk);

event.post(CGEventTapLocation::HID);
Expand Down