diff options
| author | Teddy Wing | 2018-10-18 01:18:20 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2018-10-18 01:18:20 +0200 | 
| commit | 6bf82d2e5f1a7721cee670f244ef838ef6275cbe (patch) | |
| tree | c57163b43782311fdc579a7f08f1961c6b1b055a | |
| parent | e5dae0bc586092acbd05ba70e514c62c461fcb1a (diff) | |
| download | dome-key-map-6bf82d2e5f1a7721cee670f244ef838ef6275cbe.tar.bz2 | |
Add `NXKey` to `KeyboardKeyWithModifiers`
Add this new key type to the enum and enable it to be `tap()`ped like
the others.
Made `NXKey` a type alias instead of a struct to allow us to refer to
any of the NX key constants using the `NXKey` type. This also meant
moving those NX constants outside of the `NXKey` impl and into the top
level. May end up wrapping them back up in some kind of namespace later.
Adjusted the public-ness of some functions to support this change.
| -rw-r--r-- | dome_key_map.h | 2 | ||||
| -rw-r--r-- | src/autopilot_internal.rs | 2 | ||||
| -rw-r--r-- | src/key_code.rs | 49 | ||||
| -rw-r--r-- | src/parser.rs | 15 | 
4 files changed, 41 insertions, 27 deletions
| diff --git a/dome_key_map.h b/dome_key_map.h index 2e77bc9..86bd45d 100644 --- a/dome_key_map.h +++ b/dome_key_map.h @@ -39,7 +39,7 @@ void c_run_key_action(State *state, Trigger trigger, const Trigger *mode);  void config_free(Config *ptr); -extern void dkess_press_key(int16_t key, int16_t modifier_flags); +extern void dkess_press_key(int16_t key, CGEventFlags modifier_flags);  void logger_init(void); diff --git a/src/autopilot_internal.rs b/src/autopilot_internal.rs index 519305e..2c91e84 100644 --- a/src/autopilot_internal.rs +++ b/src/autopilot_internal.rs @@ -222,7 +222,7 @@ fn cg_event_flags_from_flag(flag: Flag) -> CGEventFlags {  }  #[cfg(target_os = "macos")] -fn cg_event_mask_for_flags(flags: &[Flag]) -> CGEventFlags { +pub fn cg_event_mask_for_flags(flags: &[Flag]) -> CGEventFlags {      flags          .iter()          .map(|&x| cg_event_flags_from_flag(x)) diff --git a/src/key_code.rs b/src/key_code.rs index 3444bc9..630fbeb 100644 --- a/src/key_code.rs +++ b/src/key_code.rs @@ -8,6 +8,7 @@ use cocoa::base::{id, nil};  use cocoa::foundation::NSPoint;  use core_graphics::event::{      CGEvent, +    CGEventFlags,      // CGEventPost,      CGEventRef,      CGEventTapLocation, @@ -34,7 +35,7 @@ use core_graphics::event::{  #[link(name="dome_key_event_source_simulator", kind="static")]  extern "C" { -    fn dkess_press_key(key: i16, modifier_flags: i16); +    pub fn dkess_press_key(key: i16, modifier_flags: CGEventFlags);  }  #[cfg(test)] @@ -141,31 +142,29 @@ mod tests {  // #define NX_KEYTYPE_ILLUMINATION_TOGGLE	23 -pub struct NXKey; +pub type NXKey = i16; -impl NXKey { -    // /System/Library/Frameworks/IOKit.framework/Versions/A/Headers/hidsystem/ev_keymap.h -    pub const NX_KEYTYPE_SOUND_UP: i16 = 0; -    pub const NX_KEYTYPE_SOUND_DOWN: i16 = 1; -    pub const NX_KEYTYPE_BRIGHTNESS_UP: i16 = 2; -    pub const NX_KEYTYPE_BRIGHTNESS_DOWN: i16 = 3; -    pub const NX_KEYTYPE_HELP: i16 = 5; -    pub const NX_POWER_KEY: i16 = 6; -    pub const NX_KEYTYPE_MUTE: i16 = 7; -    pub const NX_KEYTYPE_NUM_LOCK: i16 = 10; +// /System/Library/Frameworks/IOKit.framework/Versions/A/Headers/hidsystem/ev_keymap.h +pub const NX_KEYTYPE_SOUND_UP: NXKey = 0; +pub const NX_KEYTYPE_SOUND_DOWN: NXKey = 1; +pub const NX_KEYTYPE_BRIGHTNESS_UP: NXKey = 2; +pub const NX_KEYTYPE_BRIGHTNESS_DOWN: NXKey = 3; +pub const NX_KEYTYPE_HELP: NXKey = 5; +pub const NX_POWER_KEY: NXKey = 6; +pub const NX_KEYTYPE_MUTE: NXKey = 7; +pub const NX_KEYTYPE_NUM_LOCK: NXKey = 10; -    pub const NX_KEYTYPE_CONTRAST_UP: i16 = 11; -    pub const NX_KEYTYPE_CONTRAST_DOWN: i16 = 12; -    pub const NX_KEYTYPE_EJECT: i16 = 14; -    pub const NX_KEYTYPE_VIDMIRROR: i16 = 15; +pub const NX_KEYTYPE_CONTRAST_UP: NXKey = 11; +pub const NX_KEYTYPE_CONTRAST_DOWN: NXKey = 12; +pub const NX_KEYTYPE_EJECT: NXKey = 14; +pub const NX_KEYTYPE_VIDMIRROR: NXKey = 15; -    pub const NX_KEYTYPE_PLAY: i16 = 16; -    pub const NX_KEYTYPE_NEXT: i16 = 17; -    pub const NX_KEYTYPE_PREVIOUS: i16 = 18; -    pub const NX_KEYTYPE_FAST: i16 = 19; -    pub const NX_KEYTYPE_REWIND: i16 = 20; +pub const NX_KEYTYPE_PLAY: NXKey = 16; +pub const NX_KEYTYPE_NEXT: NXKey = 17; +pub const NX_KEYTYPE_PREVIOUS: NXKey = 18; +pub const NX_KEYTYPE_FAST: NXKey = 19; +pub const NX_KEYTYPE_REWIND: NXKey = 20; -    pub const NX_KEYTYPE_ILLUMINATION_UP: i16 = 21; -    pub const NX_KEYTYPE_ILLUMINATION_DOWN: i16 = 22; -    pub const NX_KEYTYPE_ILLUMINATION_TOGGLE: i16 = 23; -} +pub const NX_KEYTYPE_ILLUMINATION_UP: NXKey = 21; +pub const NX_KEYTYPE_ILLUMINATION_DOWN: NXKey = 22; +pub const NX_KEYTYPE_ILLUMINATION_TOGGLE: NXKey = 23; diff --git a/src/parser.rs b/src/parser.rs index 3b5a8e4..95cca4d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,6 +15,9 @@ use combine::parser::char::{  use combine::parser::repeat::take_until;  use combine::stream::state::{SourcePosition, State}; +use autopilot_internal::cg_event_mask_for_flags; +use key_code::{NXKey, dkess_press_key}; +  #[repr(C)]  #[derive(Clone, Debug, Hash, Eq, PartialEq)]  pub enum HeadphoneButton { @@ -68,6 +71,7 @@ impl KeyCode {  pub enum KeyboardKey {      Character(Character),      KeyCode(KeyCode), +    NXKey(NXKey),  }  #[derive(Debug, PartialEq)] @@ -92,6 +96,17 @@ impl KeyboardKeyWithModifiers {              KeyboardKey::KeyCode(ref k) => {                  autopilot::key::tap(k.0, &self.flags, 0)              }, +            KeyboardKey::NXKey(nx) => { +                // let flags = self.flags.iter().fold( +                //     CGEventFlags::CGEventFlagNull, +                //     |acc, f| acc | cg_event_flags_from_flag(f) +                // ); +                let flags = cg_event_mask_for_flags(&self.flags); + +                unsafe { +                    dkess_press_key(nx, flags); +                } +            },          }      }  } | 
