From fa8595b77d5a22f3213937cb7efbad6a0c1146ce Mon Sep 17 00:00:00 2001 From: Asura Date: Tue, 12 Jul 2022 08:36:45 -0700 Subject: [PATCH] Supports Mac OS simulate input by scancode --- Cargo.lock | 21 +++--------------- Cargo.toml | 2 +- libs/enigo/src/macos/macos_impl.rs | 35 +++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f3895c1c..f2db3ff15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1317,7 +1317,7 @@ dependencies = [ "log", "objc", "pkg-config", - "rdev 0.5.0 (git+https://github.com/asur4s/rdev)", + "rdev", "serde 1.0.137", "serde_derive", "unicode-segmentation", @@ -3858,22 +3858,7 @@ dependencies = [ [[package]] name = "rdev" version = "0.5.0" -dependencies = [ - "cocoa 0.22.0", - "core-foundation 0.7.0", - "core-foundation-sys 0.7.0", - "core-graphics 0.19.2", - "enum-map", - "lazy_static", - "libc", - "winapi 0.3.9", - "x11", -] - -[[package]] -name = "rdev" -version = "0.5.0" -source = "git+https://github.com/asur4s/rdev#83d998895677129f0ba8fc2ada4cddd1e0df418f" +source = "git+https://github.com/asur4s/rdev#e0ed6e08b7fb7e8ac80b2ef6e710ba1db9fe0751" dependencies = [ "cocoa 0.22.0", "core-foundation 0.7.0", @@ -4126,7 +4111,7 @@ dependencies = [ "num_cpus", "objc", "parity-tokio-ipc", - "rdev 0.5.0", + "rdev", "repng", "reqwest", "rpassword 6.0.1", diff --git a/Cargo.toml b/Cargo.toml index 39789c772..264c82940 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ sciter-rs = { git = "https://github.com/open-trade/rust-sciter", branch = "dyn" sys-locale = "0.2" enigo = { path = "libs/enigo" } clipboard = { path = "libs/clipboard" } -rdev = { path = "../rdev" } +rdev = { git = "https://github.com/asur4s/rdev" } ctrlc = "3.2" arboard = "2.0" #minreq = { version = "2.4", features = ["punycode", "https-native"] } diff --git a/libs/enigo/src/macos/macos_impl.rs b/libs/enigo/src/macos/macos_impl.rs index 28c9362ed..6cae30984 100644 --- a/libs/enigo/src/macos/macos_impl.rs +++ b/libs/enigo/src/macos/macos_impl.rs @@ -1,5 +1,5 @@ use core_graphics; - +use rdev::{simulate, EventType, EventType::*, Key as RdevKey, SimulateError}; // TODO(dustin): use only the things i need use self::core_graphics::display::*; @@ -354,6 +354,11 @@ impl KeyboardControllable for Enigo { } fn key_down(&mut self, key: Key) -> crate::ResultType { + let keyboard_mode = 1; + if keyboard_mode == 1 { + self.send_rdev(&key, true); + return Ok(()); + }; let code = self.key_to_keycode(key); if code == u16::MAX { return Err("".into()); @@ -369,6 +374,11 @@ impl KeyboardControllable for Enigo { } fn key_up(&mut self, key: Key) { + let keyboard_mode = 1; + if keyboard_mode == 1 { + self.send_rdev(&key, true); + return Ok(()); + }; if let Some(src) = self.event_source.as_ref() { if let Ok(event) = CGEvent::new_keyboard_event(src.clone(), self.key_to_keycode(key), false) @@ -421,6 +431,29 @@ impl Enigo { (x, (display_height as i32) - y_inv) } + fn send_rdev(&mut self, key: &Key, is_press: bool) -> bool { + log::info!("{:?} {:?}", key, is_press); + + if let Key::Raw(keycode) = key { + let event_type = match is_press { + // todo: Acccodding to client type + true => Box::leak(Box::new(EventType::KeyPress(RdevKey::Unknown( + (*keycode).into(), + )))), + false => Box::leak(Box::new(EventType::KeyRelease(RdevKey::Unknown( + (*keycode).into(), + )))), + }; + + match simulate(event_type) { + Ok(()) => true, + Err(SimulateError) => false, + } + } else { + false + } + } + fn key_to_keycode(&mut self, key: Key) -> CGKeyCode { #[allow(deprecated)] // I mean duh, we still need to support deprecated keys until they're removed