From ee5370863669439084daeb1abd2071f8fe1246c3 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sat, 11 Feb 2023 14:52:58 +0100 Subject: [PATCH] Fix: make usable on Windows and Linux --- receiver/Cargo.toml | 4 ++++ receiver/src/keyboard.rs | 20 ++++++++++++++++++++ receiver/src/main.rs | 8 +++++--- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 receiver/src/keyboard.rs diff --git a/receiver/Cargo.toml b/receiver/Cargo.toml index f67edd9..6ff8afc 100644 --- a/receiver/Cargo.toml +++ b/receiver/Cargo.toml @@ -14,5 +14,9 @@ crypto_helper = {path = "../crypto_helper"} uuid = { version = "1.3.0", features = ["serde", "v4"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" + +[target.'cfg(unix)'.dependencies] enigo = "0.0.14" + +[target.'cfg(windows)'.dependencies] simulate = "0.3.0" diff --git a/receiver/src/keyboard.rs b/receiver/src/keyboard.rs new file mode 100644 index 0000000..eb4dcc3 --- /dev/null +++ b/receiver/src/keyboard.rs @@ -0,0 +1,20 @@ +#[cfg(target_family = "unix")] +use enigo::{KeyboardControllable}; + +pub fn type_text(text: &str) { + enigo::Enigo::default().key_sequence(text); +} + +pub fn click_tab() { + enigo::Enigo::default().key_click(enigo::Key::Tab); +} + +#[cfg(target_family = "windows")] +pub fn type_text(&self, text: &str) { + simulate::type_str(text); +} + +#[cfg(target_family = "windows")] +pub fn click_tab(&self) { + simulate::send(simulate::Key::Tab); +} \ No newline at end of file diff --git a/receiver/src/main.rs b/receiver/src/main.rs index 00138f6..acff0fc 100644 --- a/receiver/src/main.rs +++ b/receiver/src/main.rs @@ -3,6 +3,8 @@ use std::time::Duration; extern crate paho_mqtt as mqtt; +mod keyboard; + #[derive(Serialize, Deserialize, PartialEq)] enum MqttMessageKind { Time, @@ -75,9 +77,9 @@ fn main() { let time_with_comma = millis_to_string(msg.time); println!("Trying to type {time_with_comma}"); - simulate::type_str(&time_with_comma); - simulate::send(simulate::Key::Tab); - simulate::send(simulate::Key::Tab); + keyboard::type_text(&time_with_comma); + keyboard::click_tab(); + keyboard::click_tab(); let reply = MqttMessage { id: msg.id,