diff --git a/README.md b/README.md index 1b143e3..d9e80dd 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/lib.rs b/src/lib.rs index d53a51e..54e60d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,33 +47,43 @@ pub fn insert_text(text: String, insert_with_paste: Option, 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(); +} \ No newline at end of file