Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paste from clipboard #12

Merged
merged 2 commits into from
Jan 22, 2025
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
62 changes: 62 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anyhow = "1.0.75"
async-openai = "0.14.3"
async-std = "1.12.0"
clap = { version = "4.4.6", features = ["derive"] }
clipboard = "0.5.0"
cpal = "0.15.2"
dotenvy = "0.15.7"
enigo = "0.1.3"
Expand Down
49 changes: 45 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ use enigo::{Enigo, KeyboardControllable};
use std::env;
use tempfile::tempdir;
mod transcribe;
use std::thread;
use std::thread::{self, sleep};
use transcribe::trans;
mod record;
use async_std::future;
use clap::{Parser, Subcommand};
use clipboard::ClipboardContext;
use clipboard::ClipboardProvider;
use cpal::traits::{DeviceTrait, HostTrait};
use rdev::{listen, Event};
use record::rec;
use std::error::Error;
use std::time::Duration;
mod easy_rdev_key;
use crate::easy_rdev_key::PTTKey;

#[derive(Parser, Debug)]
#[command(version)]
Expand All @@ -30,7 +33,7 @@ struct Opt {

/// The push to talk key
#[arg(short, long)]
ptt_key: Option<easy_rdev_key::PTTKey>,
ptt_key: Option<PTTKey>,

/// The push to talk key.
/// Use this if you want to use a key that is not supported by the PTTKey enum.
Expand All @@ -53,6 +56,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let opt = Opt::parse();
let _ = dotenv();

let mut clipboard: ClipboardContext = ClipboardProvider::new().unwrap();

match opt.subcommands {
Some(subcommand) => {
match subcommand {
Expand All @@ -73,6 +78,9 @@ fn main() -> Result<(), Box<dyn Error>> {
SubCommands::ListDevices => {
let host = cpal::default_host();

let test = host.default_output_device().unwrap();

println!("default output_device: {:?}", test.name());
// Set up the input device and stream with the default input config.
host.default_input_device();
let devices = host
Expand Down Expand Up @@ -131,7 +139,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let runtime = tokio::runtime::Runtime::new()
.context("Failed to create tokio runtime")
.unwrap();
let mut enigo = Enigo::new();
// let mut enigo = Enigo::new();

let tmp_dir = tempdir().unwrap();
// println!("{:?}", tmp_dir.path());
Expand Down Expand Up @@ -221,7 +229,40 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("No transcription");
}

enigo.key_sequence(&transcription);
// enigo.key_sequence(&transcription);
// paste from clipboard

// get the clipboard contents so we can restore it later
let clip_tmp_result = clipboard.get_contents();

// Set and paste Clipboard Contents
match clipboard.set_contents(transcription) {
Ok(_) => {
Enigo.key_sequence_parse("{+CTRL}");
sleep(Duration::from_millis(100));
Enigo.key_sequence_parse("v");
sleep(Duration::from_millis(100));
Enigo.key_sequence_parse("{-CTRL}");
sleep(Duration::from_millis(100));

// restore the clipboard contents
if let Ok(clip_tmp) = clip_tmp_result {
if let Err(err) = clipboard.set_contents(clip_tmp) {
println!(
"Error restoring clipboard contents: {}",
err
);
}
}
}
Err(err) => {
println!(
"Error: Failed to set clipboard contents: {:?}",
err
);
continue;
}
}
} else {
println!("Recording too short");
}
Expand Down
Loading