Skip to content

Commit

Permalink
Update key in Mac and increase default copy wait time to 5ms
Browse files Browse the repository at this point in the history
  • Loading branch information
xitanggg committed Jan 1, 2024
1 parent 488cf94 commit a4b54ea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const selectionText = getSelectionText();

**Customized Usage**

`getSelectionText` accepts an optional `copyWaitTimeMs` input argument, which sets how long to wait after performing the copy operation before reading the clipboard text. It defaults to 1ms, which works for most use cases with small selection text. However, a larger value would be needed to support use case for large selection text that takes longer to copy.
`getSelectionText` accepts an optional `copyWaitTimeMs` input argument, which sets how long to wait after performing the copy operation before reading the clipboard text. It defaults to 5ms, which works for most use cases with small selection text. However, a larger value would be needed to support use case for large selection text that takes longer to copy.

```typescript
import { getSelectionText } from '@xitanggg/node-selection;

const LONG_COPY_WAIT_TIME_MS = 5;
const LONG_COPY_WAIT_TIME_MS = 10;
const selectionText = getSelectionText(LONG_COPY_WAIT_TIME_MS);
```

Expand All @@ -42,7 +42,7 @@ The implementation is written in Rust and is ~10 lines of code (see `/src/lib.rs
The selection text is retrieved in a 3 steps processes:

1. Save clipboard existing text and clear clipboard
2. Simulate `Ctrl + C` keyboard input to copy selection text to clipboard
2. Simulate `Ctrl + C` (`Cmd + C` in Mac) keyboard input to copy selection text to clipboard
3. Read clipboard to retrieve selection text and return it as result (the previous clipboard text is restored before returning to minimize side effects to users)

**Dependency**
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*
* The selection text is retrieved in a 3 steps processes:
* 1. Save clipboard existing text and clear clipboard
* 2. Simulate `Ctrl + C` keyboard input to copy selection text to clipboard
* 2. Simulate `Ctrl + C` (`Cmd + C` in Mac) keyboard input to copy selection text to clipboard
* 3. Read clipboard to retrieve selection text and return it as result
* (the previous clipboard text is restored before returning to minimize side effects to users)
*
* ##### Arguments
* * `copyWaitTimeMs` - An optional number that sets how long to wait after performing the copy
* operation before reading the clipboard text. It defaults to 1ms, which
* operation before reading the clipboard text. It defaults to 5ms, which
* works for most use cases with small selection text. However, a larger value
* would be needed to support use case for large selection text that takes
* longer to copy to the clipboard.
Expand Down
19 changes: 13 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ use enigo::{
};
use std::{thread, time};

static DEFAULT_COPY_WAIT_TIME_MS: u32 = 5;

/// Returns the current selection text. If there is no selection text, returns an empty string.
///
/// The selection text is retrieved in a 3 steps processes:
/// 1. Save clipboard existing text and clear clipboard
/// 2. Simulate `Ctrl + C` keyboard input to copy selection text to clipboard
/// 2. Simulate `Ctrl + C` (`Cmd + C` in Mac) keyboard input to copy selection text to clipboard
/// 3. Read clipboard to retrieve selection text and return it as result
/// (the previous clipboard text is restored before returning to minimize side effects to users)
///
/// ##### Arguments
/// * `copyWaitTimeMs` - An optional number that sets how long to wait after performing the copy
/// operation before reading the clipboard text. It defaults to 1ms, which
/// operation before reading the clipboard text. It defaults to 5ms, which
/// works for most use cases with small selection text. However, a larger value
/// would be needed to support use case for large selection text that takes
/// longer to copy to the clipboard.
Expand All @@ -34,14 +36,19 @@ pub fn get_selection_text(copy_wait_time_ms: Option<u32>) -> String {
// Clear clipboard
clipboard.clear().unwrap();

// Simulate Ctrl + C keyboard press to copy selection text to clipboard
// Simulate Ctrl/Cmd + C keyboard input to copy selection text to clipboard
let mut enigo = Enigo::new(&Settings::default()).unwrap();
enigo.key(Key::Control, Press).unwrap();
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('c'), Click).unwrap();
enigo.key(Key::Control, Release).unwrap();
enigo.key(control_or_command_key, Release).unwrap();

// Wait for clipboard to be updated with copied selection text
thread::sleep(time::Duration::from_millis((copy_wait_time_ms.unwrap_or(1) as u64).into()));
thread::sleep(time::Duration::from_millis(u64::from(copy_wait_time_ms.unwrap_or(DEFAULT_COPY_WAIT_TIME_MS))));

// Read clipboard to retrieve selection text
let selection_text = clipboard.get_text().unwrap_or(String::new());
Expand Down

0 comments on commit a4b54ea

Please sign in to comment.