use clipboard_rs::{ Clipboard, ClipboardContext, ClipboardHandler, ClipboardWatcher, ClipboardWatcherContext, }; use std::{thread, time::Duration}; struct Manager { ctx: ClipboardContext, } impl Manager { pub fn new() -> Self { let ctx = ClipboardContext::new().unwrap(); Manager { ctx } } } impl ClipboardHandler for Manager { fn on_clipboard_change(&mut self) { println!( "on_clipboard_change, txt = {}", self.ctx.get_text().unwrap_or("".to_string()) ); } } fn main() { let manager = Manager::new(); let mut watcher = ClipboardWatcherContext::new().unwrap(); let watcher_shutdown: clipboard_rs::WatcherShutdown = watcher.add_handler(manager).get_shutdown_channel(); thread::spawn(move || { thread::sleep(Duration::from_secs(5)); println!("stop watch!"); watcher_shutdown.stop(); }); println!("start watch!"); watcher.start_watch(); }