Add suspension (testing)

This commit is contained in:
tommy 2024-10-20 01:47:24 -04:00
parent 728c8bea02
commit 748665ef5e
Signed by: tommy
GPG Key ID: 56B522AB2EF2263A

View File

@ -1,8 +1,10 @@
use std::sync::{Arc, Mutex};
use rdev::{simulate, Event, EventType, SimulateError}; use rdev::{simulate, Event, EventType, SimulateError};
use std::process::exit; use std::process::exit;
const SPACE_KEY: rdev::Key = rdev::Key::Space; const SPACE_KEY: rdev::Key = rdev::Key::Space;
const ESCAPE_KEY: rdev::Key = rdev::Key::Escape; const ESCAPE_KEY: rdev::Key = rdev::Key::Escape;
const SUSPEND_KEY: rdev::Key = rdev::Key::SemiColon;
fn send(event_type: EventType) { fn send(event_type: EventType) {
match simulate(&event_type) { match simulate(&event_type) {
@ -14,18 +16,36 @@ fn send(event_type: EventType) {
} }
fn main() { fn main() {
let callback = |event: Event| { let suspended = Arc::new(Mutex::new(false));
let suspended_clone = Arc::clone(&suspended);
let callback = move |event: Event| {
let mut matched: bool = false; let mut matched: bool = false;
match event.event_type { match event.event_type {
EventType::KeyPress(SPACE_KEY) => { EventType::KeyPress(key) if key == SPACE_KEY => {
let suspended = suspended.lock().unwrap();
if *suspended {
return Some(event);
} else {
send(EventType::ButtonPress(rdev::Button::Left)); send(EventType::ButtonPress(rdev::Button::Left));
matched = true; matched = true;
}
}, },
EventType::KeyRelease(SPACE_KEY) => { EventType::KeyRelease(key) if key == SPACE_KEY => {
let suspended = suspended.lock().unwrap();
if *suspended {
return Some(event);
} else {
send(EventType::ButtonRelease(rdev::Button::Left)); send(EventType::ButtonRelease(rdev::Button::Left));
matched = true; matched = true;
}
}, },
EventType::KeyPress(ESCAPE_KEY) => exit(0), 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 { if !matched {
@ -35,7 +55,7 @@ fn main() {
} }
}; };
println!("Escape: Quit program\nSpace: Left mouse click"); println!("Escape: Quit program\nSemi colon (;): Suspend\nSpace: Left mouse click");
if let Err(error) = rdev::grab(callback) { if let Err(error) = rdev::grab(callback) {
eprintln!("Error: {:?}", error); eprintln!("Error: {:?}", error);