Skip to content

Commit cc235fc

Browse files
committed
Merge branch 'main' of github.com:cjpais/Handy
2 parents 9455c77 + 464ce61 commit cc235fc

4 files changed

Lines changed: 119 additions & 28 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 71 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tokio = "1.43.0"
4040
vad-rs = "0.1.5"
4141
tauri-plugin-store = "2"
4242
tauri-plugin-os = "2"
43+
enigo = "0.5.0"
4344

4445
[target.'cfg(target_os = "macos")'.dependencies]
4546
whisper-rs = { version = "0.13.2", features = ["metal"] }

src-tauri/src/actions.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ impl ShortcutAction for TranscribeAction {
4343
Ok(transcription) => {
4444
println!("Transcription Result: {}", transcription);
4545
if !transcription.is_empty() {
46-
utils::paste(transcription, ah);
46+
let transcription_clone = transcription.clone();
47+
let ah_clone = ah.clone();
48+
ah.run_on_main_thread(move || {
49+
match utils::paste(transcription_clone, ah_clone) {
50+
Ok(()) => println!("Text pasted successfully"),
51+
Err(e) => eprintln!("Failed to paste transcription: {}", e),
52+
}
53+
})
54+
.unwrap_or_else(|e| {
55+
eprintln!("Failed to run paste on main thread: {:?}", e);
56+
});
4757
}
4858
}
4959
Err(err) => println!("Global Shortcut Transcription error: {}", err),

src-tauri/src/utils.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,61 @@
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+
46
use tauri::image::Image;
57
use tauri::tray::TrayIcon;
68
use tauri::AppHandle;
79
use tauri::Manager;
810
use tauri_plugin_clipboard_manager::ClipboardExt;
911

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> {
2313
// Determine the modifier key based on the OS
2414
#[cfg(target_os = "macos")]
25-
let modifier_key = Key::MetaLeft; // Command key on macOS
15+
let modifier_key = Key::Meta; // Command key on macOS
2616
#[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))?;
2821

2922
// 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))?;
3229

3330
// 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(())
3639
}
3740

38-
pub fn paste(text: String, app_handle: AppHandle) {
41+
pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {
3942
let clipboard = app_handle.clipboard();
4043

4144
// get the current clipboard content
4245
let clipboard_content = clipboard.read_text().unwrap_or_default();
4346

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()?;
4652

4753
// 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(())
4959
}
5060

5161
pub enum TrayIconState {

0 commit comments

Comments
 (0)