Skip to content

Commit

Permalink
added fix for 20 character limitation on macOS. Should work with unic…
Browse files Browse the repository at this point in the history
…ode graphemes clusters as well.
  • Loading branch information
Dustin Bensing authored and Dustin Bensing committed Nov 12, 2019
1 parent 3e692c0 commit 949da70
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ winapi = { version = "0.3.8", features = ["winuser"] }
[target.'cfg(target_os = "macos")'.dependencies]
core-graphics = "0.18.0"
objc = "0.2.7"
unicode-segmentation = "1.6.0"

[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2.65"
Expand Down
2 changes: 1 addition & 1 deletion examples/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
let mut enigo = Enigo::new();

// write text
enigo.key_sequence("Hello World! ❤️");
enigo.key_sequence("Hello World! here is a lot of text ❤️");

// select all
enigo.key_down(Key::Control);
Expand Down
15 changes: 11 additions & 4 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,17 @@ impl MouseControllable for Enigo {

impl KeyboardControllable for Enigo {
fn key_sequence(&mut self, sequence: &str) {
let event = CGEvent::new_keyboard_event(self.event_source.clone(), 0, true)
.expect("Failed creating event");
event.set_string(sequence);
event.post(CGEventTapLocation::HID);
// NOTE(dustin): This is a fix for issue https://github.com/enigo-rs/enigo/issues/68
// TODO(dustin): This could be improved by aggregating 20 bytes worth of graphemes at a time
// but i am unsure what would happen for grapheme clusters greater than 20 bytes ...
use unicode_segmentation::UnicodeSegmentation;
let clusters = UnicodeSegmentation::graphemes(sequence, true).collect::<Vec<&str>>();
for cluster in clusters {
let event = CGEvent::new_keyboard_event(self.event_source.clone(), 0, true)
.expect("Failed creating event");
event.set_string(cluster);
event.post(CGEventTapLocation::HID);
}
}

fn key_click(&mut self, key: Key) {
Expand Down

0 comments on commit 949da70

Please sign in to comment.