Skip to content

Commit

Permalink
macOS: Added workaround for entering text that starts with newline ch…
Browse files Browse the repository at this point in the history
…aracters

Fixes #260
  • Loading branch information
pentamassiv committed Feb 6, 2024
1 parent e742cc6 commit b460af2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit b460af2

Please sign in to comment.