From 52398f0c8507fdb5fe4a680bbbb841232b396ae7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 3 Oct 2018 12:42:00 +0200 Subject: run_key_action_for_mode(): Tap keys using `KeyboardKeyWithModifiers` Use `autopilot::key::tap()` to simulate each key and modifier in an action. Originally tried to do this using a method that takes a closure to access the `KeyCodeConvertible` types inside `KeyboardKey`. Ended up with a much cleaner API solution that only requires a single method call. --- src/cocoa_bridge.rs | 12 ++++++++++++ src/parser.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index 68a8515..c2e493b 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -300,6 +300,12 @@ mode { .with_action(s) .in_mode(trigger) ) + } else if let Action::Map(action) = &map.action { + for key in action { + key.tap() + } + + None } else { None } @@ -327,6 +333,12 @@ mode { KeyActionResult::new(ActionKind::Map) .with_action(s) ) + } else if let Action::Map(action) = &map.action { + for key in action { + key.tap() + } + + None } else { None } diff --git a/src/parser.rs b/src/parser.rs index 9e11175..daea8fc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -65,13 +65,35 @@ impl KeyCode { } #[derive(Debug, PartialEq)] -enum KeyboardKey { +pub enum KeyboardKey { Character(Character), KeyCode(KeyCode), } +impl KeyboardKey { + // fn map(&self, f: F) -> &Self + // where + // K: KeyCodeConvertible, + // F: Fn(K), + // { + // match self { + // KeyboardKey::Character(c) => f(c.0), + // KeyboardKey::KeyCode(k) => f(k.0), + // } + // + // self + // } + + // pub fn extract(&self) -> Box { + // return match self { + // KeyboardKey::Character(c) => Box::new(c.0), + // KeyboardKey::KeyCode(k) => Box::new(k.0), + // } + // } +} + #[derive(Debug, PartialEq)] -struct KeyboardKeyWithModifiers { +pub struct KeyboardKeyWithModifiers { key: KeyboardKey, flags: Vec, } @@ -83,6 +105,17 @@ impl KeyboardKeyWithModifiers { flags: modifiers, } } + + pub fn tap(&self) { + match self.key { + KeyboardKey::Character(ref c) => { + autopilot::key::tap(c.0, &self.flags, 0) + }, + KeyboardKey::KeyCode(ref k) => { + autopilot::key::tap(k.0, &self.flags, 0) + }, + } + } } #[derive(Debug, PartialEq)] -- cgit v1.2.3