Add suspension
This commit is contained in:
parent
728c8bea02
commit
bc62255848
64
src/main.rs
64
src/main.rs
@ -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,30 +16,48 @@ fn send(event_type: EventType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let callback = |event: Event| {
|
let suspended = Arc::new(Mutex::new(false));
|
||||||
let mut matched: bool = false;
|
let suspended_clone = Arc::clone(&suspended);
|
||||||
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
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
eprintln!("Error: {:?}", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user