-
Notifications
You must be signed in to change notification settings - Fork 107
/
serde.rs
36 lines (32 loc) · 1.4 KB
/
serde.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use enigo::{
agent::{Agent, Token},
Button, Enigo, Key, Settings,
};
use std::{thread, time::Duration};
fn main() {
env_logger::init();
thread::sleep(Duration::from_secs(2));
let mut enigo = Enigo::new(&Settings::default()).unwrap();
// write text, move the mouse (10/10) relative from the cursors position, scroll
// down, enter the unicode U+1F525 (🔥) and then select all
let tokens = vec![
Token::Text("Hello World! ❤️".to_string()),
Token::MoveMouse(10, 10, enigo::Coordinate::Rel),
Token::Scroll(5, enigo::Axis::Vertical),
Token::Button(Button::Left, enigo::Direction::Click),
Token::Key(Key::Unicode('🔥'), enigo::Direction::Click),
Token::Key(Key::Control, enigo::Direction::Press),
Token::Key(Key::Unicode('a'), enigo::Direction::Click),
Token::Key(Key::Control, enigo::Direction::Release),
];
// There are serde aliases so you could also deserialize the same tokens from
// the following string let serialized=r#"[t("Hello World!
// ❤\u{fe0f}"),m(10,10,r),s(5),b(l),k(uni('🔥')),k(ctrl,p),k(uni('a')),
// k(ctrl,r)]"#.to_string();
let serialized = ron::to_string(&tokens).unwrap();
println!("serialized = {serialized}");
let deserialized_tokens: Vec<_> = ron::from_str(&serialized).unwrap();
for token in &deserialized_tokens {
enigo.execute(token).unwrap();
}
}