use keyflow::prelude::*;
use serde::{Deserialize, Serialize};
use std::fs;
use std::thread;
use std::time::Duration;
#[derive(Debug, Serialize, Deserialize)]
struct AutomationScript {
name: String,
description: String,
events: Vec<InputEvent>,
}
#[derive(Debug, Serialize, Deserialize)]
struct HotkeyConfig {
id: String,
description: String,
key: Key,
modifiers: Vec<Key>,
}
fn main() -> Result<()> {
Keyflow::builder()
.with_screen(Screen::default())
.with_hotkeys()
.initialize()?;
thread::sleep(Duration::from_secs(3));
#[rustfmt::skip]
let type_script = AutomationScript {
name: "hello_keyflow".to_string(),
description: "Types 'Hello Keyflow' with delays".to_string(),
events: vec![
InputEvent::KeyEvent { key: Key::ShiftLeft, action: Action::Press, },
InputEvent::KeyEvent { key: Key::H, action: Action::Click, },
InputEvent::KeyEvent { key: Key::ShiftLeft, action: Action::Release, },
InputEvent::KeyEvent { key: Key::E, action: Action::Click, },
InputEvent::KeyEvent { key: Key::L, action: Action::Click, },
InputEvent::KeyEvent { key: Key::L, action: Action::Click, },
InputEvent::KeyEvent { key: Key::O, action: Action::Click, },
InputEvent::KeyEvent { key: Key::Space, action: Action::Click, },
InputEvent::Delay(Duration::from_millis(1000)),
InputEvent::KeyEvent { key: Key::ShiftLeft, action: Action::Press, },
InputEvent::KeyEvent { key: Key::K, action: Action::Click, },
InputEvent::KeyEvent { key: Key::ShiftLeft, action: Action::Release, },
InputEvent::KeyEvent { key: Key::E, action: Action::Click, },
InputEvent::KeyEvent { key: Key::Y, action: Action::Click, },
InputEvent::KeyEvent { key: Key::F, action: Action::Click, },
InputEvent::KeyEvent { key: Key::L, action: Action::Click, },
InputEvent::KeyEvent { key: Key::O, action: Action::Click, },
InputEvent::KeyEvent { key: Key::W, action: Action::Click, },
],
};
let type_json = serde_json::to_string_pretty(&type_script).expect("pretty print failed");
fs::write("automation_script.json", &type_json)?;
let loaded_json = fs::read_to_string("automation_script.json")?;
let loaded_type_script: AutomationScript =
serde_json::from_str(&loaded_json).expect("could not deserialize json from string");
Keyflow::batch(loaded_type_script.events);
thread::sleep(Duration::from_secs(2));
let hotkey_configs = vec![
HotkeyConfig {
id: "save".to_string(),
description: "Save file".to_string(),
key: Key::S,
modifiers: vec![Key::ControlLeft],
},
HotkeyConfig {
id: "quit".to_string(),
description: "Quit application".to_string(),
key: Key::Q,
modifiers: vec![Key::ControlLeft],
},
HotkeyConfig {
id: "test".to_string(),
description: "Run test".to_string(),
key: Key::T,
modifiers: vec![Key::ControlLeft, Key::AltLeft],
},
];
let hotkeys_json = serde_json::to_string_pretty(&hotkey_configs).expect("pretty print failed");
fs::write("hotkey_config.json", &hotkeys_json)?;
let mouse_script = AutomationScript {
name: "mouse_automation".to_string(),
description: "Moves mouse and clicks at specific positions".to_string(),
events: vec![
InputEvent::MouseMove(Movement::Absolute { x: 500, y: 300 }),
InputEvent::Delay(Duration::from_millis(200)),
InputEvent::ButtonEvent {
button: Button::Left,
action: Action::Click,
},
InputEvent::Delay(Duration::from_millis(500)),
InputEvent::MouseMove(Movement::Relative { dx: 100, dy: 50 }),
InputEvent::Delay(Duration::from_millis(200)),
InputEvent::ButtonEvent {
button: Button::Right,
action: Action::Click,
},
InputEvent::MouseScroll(Scroll::Vertical(-3)),
],
};
let mouse_json = serde_json::to_string_pretty(&mouse_script).expect("pretty print failed");
fs::write("mouse_automation.json", &mouse_json)?;
Ok(())
}