espanso/src/keyboard/mod.rs

78 lines
1.6 KiB
Rust
Raw Normal View History

#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "linux")]
mod linux;
2019-09-05 15:20:52 +00:00
#[cfg(target_os = "macos")]
mod macos;
use std::sync::mpsc;
2019-09-05 18:54:19 +00:00
use serde::{Serialize, Deserialize};
2019-08-30 19:24:03 +00:00
pub trait KeyboardInterceptor {
fn initialize(&self);
fn start(&self);
}
2019-09-06 14:06:41 +00:00
pub trait KeyboardSender {
fn send_string(&self, s: &str);
fn send_enter(&self);
fn delete_string(&self, count: i32);
}
2019-09-05 18:54:19 +00:00
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2019-09-05 17:18:55 +00:00
pub enum KeyModifier {
CTRL,
SHIFT,
ALT,
META,
BACKSPACE,
}
2019-09-05 18:54:19 +00:00
impl Default for KeyModifier {
fn default() -> Self {
KeyModifier::ALT
}
}
2019-09-05 17:18:55 +00:00
#[derive(Debug)]
pub enum KeyEvent {
Char(char),
Modifier(KeyModifier)
}
// WINDOWS IMPLEMENTATIONS
#[cfg(target_os = "windows")]
2019-09-05 17:18:55 +00:00
pub fn get_interceptor(sender: mpsc::Sender<KeyEvent>) -> impl KeyboardInterceptor {
windows::WindowsKeyboardInterceptor {sender}
}
#[cfg(target_os = "windows")]
pub fn get_sender() -> impl KeyboardSender {
windows::WindowsKeyboardSender{}
}
// LINUX IMPLEMENTATIONS
#[cfg(target_os = "linux")]
2019-09-05 17:18:55 +00:00
pub fn get_interceptor(sender: mpsc::Sender<KeyEvent>) -> impl KeyboardInterceptor {
linux::LinuxKeyboardInterceptor {sender}
}
#[cfg(target_os = "linux")]
pub fn get_sender() -> impl KeyboardSender {
linux::LinuxKeyboardSender{}
2019-09-05 15:20:52 +00:00
}
// MAC IMPLEMENTATION
#[cfg(target_os = "macos")]
2019-09-05 17:18:55 +00:00
pub fn get_interceptor(sender: mpsc::Sender<KeyEvent>) -> impl KeyboardInterceptor {
2019-09-05 15:20:52 +00:00
macos::MacKeyboardInterceptor {sender}
}
#[cfg(target_os = "macos")]
pub fn get_sender() -> impl KeyboardSender {
macos::MacKeyboardSender{}
}