|
1 | | -use rdev::{simulate, EventType, Key, SimulateError}; |
2 | | -use std::thread; |
3 | | -use std::time; |
| 1 | +use enigo::Enigo; |
| 2 | +use enigo::Key; |
| 3 | +use enigo::Keyboard; |
| 4 | +use enigo::Settings; |
| 5 | + |
4 | 6 | use tauri::image::Image; |
5 | 7 | use tauri::tray::TrayIcon; |
6 | 8 | use tauri::AppHandle; |
7 | 9 | use tauri::Manager; |
8 | 10 | use tauri_plugin_clipboard_manager::ClipboardExt; |
9 | 11 |
|
10 | | -fn try_send_event(event: &EventType) { |
11 | | - if let Err(SimulateError) = simulate(event) { |
12 | | - println!("We could not send {:?}", event); |
13 | | - } |
14 | | -} |
15 | | - |
16 | | -fn send_with_delay(event: EventType, delay_ms: u64) { |
17 | | - try_send_event(&event); |
18 | | - thread::sleep(time::Duration::from_millis(delay_ms)); |
19 | | -} |
20 | | - |
21 | | -// TODO: use enigo for paste not on macos? |
22 | | -fn send_paste() { |
| 12 | +fn send_paste() -> Result<(), String> { |
23 | 13 | // Determine the modifier key based on the OS |
24 | 14 | #[cfg(target_os = "macos")] |
25 | | - let modifier_key = Key::MetaLeft; // Command key on macOS |
| 15 | + let modifier_key = Key::Meta; // Command key on macOS |
26 | 16 | #[cfg(not(target_os = "macos"))] |
27 | | - let modifier_key = Key::ControlLeft; // Control key on other systems |
| 17 | + let modifier_key = Key::Control; // Control key on other systems |
| 18 | + |
| 19 | + let mut enigo = Enigo::new(&Settings::default()) |
| 20 | + .map_err(|e| format!("Failed to initialize Enigo: {}", e))?; |
28 | 21 |
|
29 | 22 | // Press both keys |
30 | | - send_with_delay(EventType::KeyPress(modifier_key), 100); |
31 | | - send_with_delay(EventType::KeyPress(Key::KeyV), 100); |
| 23 | + enigo |
| 24 | + .key(modifier_key, enigo::Direction::Press) |
| 25 | + .map_err(|e| format!("Failed to press modifier key: {}", e))?; |
| 26 | + enigo |
| 27 | + .key(Key::Unicode('v'), enigo::Direction::Press) |
| 28 | + .map_err(|e| format!("Failed to press V key: {}", e))?; |
32 | 29 |
|
33 | 30 | // Release both keys |
34 | | - send_with_delay(EventType::KeyRelease(Key::KeyV), 100); |
35 | | - send_with_delay(EventType::KeyRelease(modifier_key), 0); |
| 31 | + enigo |
| 32 | + .key(Key::Unicode('v'), enigo::Direction::Release) |
| 33 | + .map_err(|e| format!("Failed to release V key: {}", e))?; |
| 34 | + enigo |
| 35 | + .key(modifier_key, enigo::Direction::Release) |
| 36 | + .map_err(|e| format!("Failed to release modifier key: {}", e))?; |
| 37 | + |
| 38 | + Ok(()) |
36 | 39 | } |
37 | 40 |
|
38 | | -pub fn paste(text: String, app_handle: AppHandle) { |
| 41 | +pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> { |
39 | 42 | let clipboard = app_handle.clipboard(); |
40 | 43 |
|
41 | 44 | // get the current clipboard content |
42 | 45 | let clipboard_content = clipboard.read_text().unwrap_or_default(); |
43 | 46 |
|
44 | | - clipboard.write_text(&text).unwrap(); |
45 | | - send_paste(); |
| 47 | + clipboard |
| 48 | + .write_text(&text) |
| 49 | + .map_err(|e| format!("Failed to write to clipboard: {}", e))?; |
| 50 | + |
| 51 | + send_paste()?; |
46 | 52 |
|
47 | 53 | // restore the clipboard |
48 | | - clipboard.write_text(&clipboard_content).unwrap(); |
| 54 | + clipboard |
| 55 | + .write_text(&clipboard_content) |
| 56 | + .map_err(|e| format!("Failed to restore clipboard: {}", e))?; |
| 57 | + |
| 58 | + Ok(()) |
49 | 59 | } |
50 | 60 |
|
51 | 61 | pub enum TrayIconState { |
|
0 commit comments