Add suspension
This commit is contained in:
parent
728c8bea02
commit
bc62255848
30
src/main.rs
30
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,18 +16,36 @@ fn send(event_type: EventType) {
|
||||
}
|
||||
|
||||
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;
|
||||
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));
|
||||
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));
|
||||
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 {
|
||||
@ -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) {
|
||||
eprintln!("Error: {:?}", error);
|
||||
|
Loading…
Reference in New Issue
Block a user