Skip to content

Commit

Permalink
Add option to perform paste if text is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
xitanggg committed Jun 16, 2024
1 parent 1f3a68d commit e601ad4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ insertText("👋Hello World! This line is inserted programmatically🤖");
`insertText` can accept 3 arguments

1. `text` - Text to be inserted
2. `insertWithPaste` - An optional boolean that sets whether to insert text with the paste method. Default to false. (Setting true to use the paste method is sometimes useful when the default insert method doesn't work for certain apps)
2. `insertWithPaste` - An optional boolean that sets whether to insert text with the paste method. Default to false. (Setting true to use the paste method is sometimes useful when the default insert method doesn't work for certain apps. Note this feature is experimental)
3. `arrowKeyToClickBeforeInsert` - An optional string that sets which arrow key to click before inserting text. Can be either "left" or "right". Default to None.

## 💡Implementation
Expand All @@ -34,7 +34,7 @@ The implementation is written in Rust and is ~20 lines of code (see `/src/lib.rs

The happy path is a wrapper of the `text` function of [enigo](https://github.com/enigo-rs/enigo), which is a cross platform input simulation library in Rust. The default behavior simply calls `enigo.text` to perform text insertion, which works for most use cases.

However, some apps might not support text insertion, so a `insertWithPaste` fallback option is provided, which inserts text via clipboard paste in a 4 steps processes:
However, some apps might not support text insertion, so a `insertWithPaste` experimental fallback option is provided, which inserts text via clipboard paste in a 4 steps processes:

1. Save clipboard existing text
2. Set text to be inserted to clipboard
Expand Down
62 changes: 36 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,43 @@ pub fn insert_text(text: String, insert_with_paste: Option<bool>, arrow_key_to_c

let insert_with_paste = insert_with_paste.unwrap_or(false);
if insert_with_paste{
let mut clipboard = Clipboard::new().unwrap();

// Save clipboard existing text
let clipboard_existing_text = clipboard.get_text().unwrap_or(String::new());

// Set insert text to clipboard
clipboard.set_text(&text).unwrap();

// Wait for clipboard to be updated with copied insert text
thread::sleep(time::Duration::from_millis(u64::from(copy_wait_time_ms.unwrap_or(DEFAULT_PASTE_WAIT_TIME_MS))));

// Simulate Ctrl/Cmd + V keyboard input to paste text
let control_or_command_key = if cfg!(target_os = "macos") {
Key::Meta
} else {
Key::Control
};
enigo.key(control_or_command_key, Press).unwrap();
enigo.key(Key::Unicode('v'), Click).unwrap();
enigo.key(control_or_command_key, Release).unwrap();

// Wait for paste to be processed
thread::sleep(time::Duration::from_millis(u64::from(paste_wait_time_ms.unwrap_or(DEFAULT_COPY_WAIT_TIME_MS))));

// Restore clipboard previous existing text to minimize side effects to users
clipboard.set_text(&clipboard_existing_text).unwrap();
// If input text is empty, we simply perform paste, with the assumption that user would
// handle setting the insert text to clipboard and restoring the clipboard state themselves
if text.is_empty(){
paste(&mut enigo);
}else{
let mut clipboard = Clipboard::new().unwrap();

// Save clipboard existing text
let clipboard_existing_text = clipboard.get_text().unwrap_or(String::new());

// Set insert text to clipboard
clipboard.set_text(&text).unwrap();

// Wait for clipboard to be updated with copied insert text
thread::sleep(time::Duration::from_millis(u64::from(copy_wait_time_ms.unwrap_or(DEFAULT_PASTE_WAIT_TIME_MS))));

paste(&mut enigo);

// Wait for paste to be processed
thread::sleep(time::Duration::from_millis(u64::from(paste_wait_time_ms.unwrap_or(DEFAULT_COPY_WAIT_TIME_MS))));

// Restore clipboard previous existing text to minimize side effects to users
clipboard.set_text(&clipboard_existing_text).unwrap();
}
}else{
enigo.text(&text).unwrap();
}
}

// Simulate Ctrl/Cmd + V keyboard input to paste text from clipboard
fn paste(enigo: &mut Enigo){
let control_or_command_key = if cfg!(target_os = "macos") {
Key::Meta
} else {
Key::Control
};
enigo.key(control_or_command_key, Press).unwrap();
enigo.key(Key::Unicode('v'), Click).unwrap();
enigo.key(control_or_command_key, Release).unwrap();
}

0 comments on commit e601ad4

Please sign in to comment.