diff --git a/src/main.rs b/src/main.rs index e18ca6c..81890fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ +use std::sync::{Arc, Mutex}; use rdev::{simulate, Event, EventType, SimulateError}; use std::process::exit; const SPACE_KEY: rdev::Key = rdev::Key::Space; const ESCAPE_KEY: rdev::Key = rdev::Key::Escape; +const SUSPEND_KEY: rdev::Key = rdev::Key::SemiColon; fn send(event_type: EventType) { match simulate(&event_type) { @@ -14,30 +16,48 @@ fn send(event_type: EventType) { } fn main() { - let callback = |event: Event| { - let mut matched: bool = false; - match event.event_type { - EventType::KeyPress(SPACE_KEY) => { - send(EventType::ButtonPress(rdev::Button::Left)); - matched = true; - }, - EventType::KeyRelease(SPACE_KEY) => { - send(EventType::ButtonRelease(rdev::Button::Left)); - matched = true; - }, - EventType::KeyPress(ESCAPE_KEY) => exit(0), - _ => () - } - if !matched { - Some(event) - } else { - None - } - }; + let suspended = Arc::new(Mutex::new(false)); + let suspended_clone = Arc::clone(&suspended); - println!("Escape: Quit program\nSpace: Left mouse click"); + let callback = move |event: Event| { + let mut matched: bool = false; + match event.event_type { + EventType::KeyPress(key) if key == SPACE_KEY => { + let suspended = suspended.lock().unwrap(); + if *suspended { + return Some(event); + } else { + send(EventType::ButtonPress(rdev::Button::Left)); + matched = true; + } + }, + EventType::KeyRelease(key) if key == SPACE_KEY => { + let suspended = suspended.lock().unwrap(); + if *suspended { + return Some(event); + } else { + send(EventType::ButtonRelease(rdev::Button::Left)); + matched = true; + } + }, + EventType::KeyPress(key) if key == SUSPEND_KEY => { + let mut suspended = suspended_clone.lock().unwrap(); + *suspended = !*suspended; + println!("Suspend: {}", *suspended); + } + EventType::KeyPress(key) if key == ESCAPE_KEY => exit(0), + _ => () + } + if !matched { + Some(event) + } else { + None + } + }; - if let Err(error) = rdev::grab(callback) { + println!("Escape: Quit program\nSemi colon (;): Suspend\nSpace: Left mouse click"); + + if let Err(error) = rdev::grab(callback) { eprintln!("Error: {:?}", error); } } \ No newline at end of file