Skip to content
This repository has been archived by the owner on May 20, 2023. It is now read-only.

windows: simulate keyboard operation Ctrl+c #4

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 65 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ arboard = "3.2.0"
clap = { version = "4.1.4", features = ["derive"] }
wry = "0.26.0"
x11-clipboard = "0.7.0"

[target.'cfg(windows)'.dependencies]
enigo = { git = "https://github.com/enigo-rs/enigo" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end, { desc = "qwe" })
- [x] 从粘贴板读取数据
- [ ] 读取鼠标选择的数据
- [x] Linux
- [ ] Windows
- [x] Windows
- [ ] MacOS
- [ ] close
- [ ] 全程只使用一个 webview
Expand Down
30 changes: 19 additions & 11 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ pub fn read_text() -> Result<String, String> {
use x11_clipboard::Clipboard;

let clipboard = Clipboard::new().unwrap();
if let Ok(curr) = clipboard.load(
clipboard.getter.atoms.primary,
clipboard.getter.atoms.utf8_string,
clipboard.getter.atoms.property,
Duration::from_millis(100),
) {
let curr = String::from_utf8_lossy(&curr)
.trim_matches('\u{0}')
.trim()
.to_string();
if
let Ok(curr) = clipboard.load(
clipboard.getter.atoms.primary,
clipboard.getter.atoms.utf8_string,
clipboard.getter.atoms.property,
Duration::from_millis(100)
)
{
let curr = String::from_utf8_lossy(&curr).trim_matches('\u{0}').trim().to_string();
if !curr.is_empty() {
Ok(curr)
} else {
Expand All @@ -31,6 +30,15 @@ pub fn read_text() -> Result<String, String> {

#[cfg(target_os = "windows")]
pub fn read_text() -> Result<String, String> {
use enigo::{ Enigo, Key, KeyboardControllable };
// simulate keyboard operation `Ctrl+c`
let mut enigo = Enigo::new();

std::thread::sleep(std::time::Duration::from_millis(200));
enigo.key_down(Key::Control);
enigo.key_click(Key::Layout('c'));
enigo.key_up(Key::Control);

read_text_cross()
}

Expand All @@ -42,4 +50,4 @@ fn read_text_cross() -> Result<String, String> {
// fn write_text(text: String) -> Result<(), String> {
// let mut clipboard = arboard::Clipboard::new().unwrap();
// clipboard.set_text(text).map_err(|err| err.to_string())
// }
// }