use enigo::*;
use crate::enums::{Key, KeyLocation};
use super::keymap::KeyMapper;
pub struct KeyboardSimulator {
key_mapper: KeyMapper,
}
impl KeyboardSimulator {
pub fn new() -> Self {
KeyboardSimulator {
key_mapper: KeyMapper::new(),
}
}
pub fn press_key(&self, enigo: &mut Enigo, key: Key, location: Option<KeyLocation>) {
self.key_down(enigo, key, location.clone());
self.key_up(enigo, key, location);
}
pub fn key_down(&self, enigo: &mut Enigo, key: Key, location: Option<KeyLocation>) {
if let Some(key_code) = self.key_mapper.map_key(key, location) {
enigo.key(key_code, Direction::Press);
}
}
pub fn key_up(&self, enigo: &mut Enigo, key: Key, location: Option<KeyLocation>) {
if let Some(key_code) = self.key_mapper.map_key(key, location) {
enigo.key(key_code, Direction::Release);
}
}
pub fn type_text(&self, enigo: &mut Enigo, text: &str) {
enigo.text(text);
}
pub fn hotkey(
&self,
enigo: &mut Enigo,
modifiers: &[Key],
key: Key,
location: Option<KeyLocation>
) {
for modifier in modifiers {
self.key_down(enigo, *modifier, None);
}
self.press_key(enigo, key, location);
for modifier in modifiers.iter().rev() {
self.key_up(enigo, *modifier, None);
}
}
}