diff --git a/keys.txt b/keys.txt new file mode 100644 index 0000000..41cbc56 --- /dev/null +++ b/keys.txt @@ -0,0 +1,179 @@ + Finished dev [unoptimized + debuginfo] target(s) in 0.07s + Running `target/debug/rusty-keys` +chosen config file: keymap.toml +Created event tap... +KEY 53 +KEY 18 +KEY 19 +KEY 20 +KEY 21 +KEY 23 +KEY 22 +KEY 26 +KEY 28 +KEY 25 +KEY 29 +KEY 27 +KEY 24 +KEY 51 +KEY 48 +KEY 12 +KEY 13 +KEY 14 +KEY 15 +KEY 17 +KEY 16 +KEY 32 +KEY 34 +KEY 31 +KEY 35 +KEY 33 +KEY 30 +KEY 36 +KEY 59 +KEY 59 +KEY 0 +KEY 1 +KEY 2 +KEY 3 +KEY 5 +KEY 4 +KEY 38 +KEY 40 +KEY 37 +KEY 41 +KEY 39 +KEY 50 +KEY 56 +KEY 56 +KEY 42 +KEY 6 +KEY 7 +KEY 8 +KEY 9 +KEY 11 +KEY 45 +KEY 46 +KEY 43 +KEY 47 +KEY 44 +KEY 60 +KEY 60 +KEY 67 +KEY 58 +KEY 58 +KEY 49 +KEY 122 +KEY 120 +KEY 99 +KEY 118 +KEY 96 +KEY 97 +KEY 98 +KEY 100 +KEY 101 +KEY 109 +KEY 71 +KEY 107 +KEY 89 +KEY 91 +KEY 92 +KEY 78 +KEY 107 +KEY 86 +KEY 87 +KEY 88 +KEY 69 +KEY 83 +KEY 84 +KEY 85 +KEY 82 +KEY 65 +KEY 10 +KEY 103 +KEY 111 +KEY 76 +KEY 62 +KEY 62 +KEY 75 +KEY 105 +KEY 61 +KEY 61 +KEY 113 +KEY 115 +KEY 126 +KEY 116 +KEY 123 +KEY 124 +KEY 119 +KEY 125 +KEY 121 +KEY 114 +KEY 117 +KEY 105 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 54 +KEY 113 +KEY 54 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 71 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 55 +KEY 55 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 55 +KEY 55 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 54 +KEY 54 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 113 +KEY 110 +KEY 71 diff --git a/out2.txt b/out2.txt new file mode 100644 index 0000000..a4e2981 --- /dev/null +++ b/out2.txt @@ -0,0 +1,120 @@ +KEY 0x0035 +KEY 0x0012 +KEY 0x0013 +KEY 0x0014 +KEY 0x0015 +KEY 0x0017 +KEY 0x0016 +KEY 0x001A +KEY 0x001C +KEY 0x0019 +KEY 0x001D +KEY 0x001B +KEY 0x0018 +KEY 0x0033 +KEY 0x0030 +KEY 0x000C +KEY 0x000D +KEY 0x000E +KEY 0x000F +KEY 0x0011 +KEY 0x0010 +KEY 0x0020 +KEY 0x0022 +KEY 0x001F +KEY 0x0023 +KEY 0x0021 +KEY 0x001E +KEY 0x0024 +KEY 0x003B +KEY 0x003B +KEY 0x0000 +KEY 0x0001 +KEY 0x0002 +KEY 0x0003 +KEY 0x0005 +KEY 0x0004 +KEY 0x0026 +KEY 0x0028 +KEY 0x0025 +KEY 0x0029 +KEY 0x0027 +KEY 0x0032 +KEY 0x0038 +KEY 0x0038 +KEY 0x002A +KEY 0x0006 +KEY 0x0007 +KEY 0x0008 +KEY 0x0009 +KEY 0x000B +KEY 0x002D +KEY 0x002E +KEY 0x002B +KEY 0x002F +KEY 0x002C +KEY 0x003C +KEY 0x003C +KEY 0x0043 +KEY 0x003A +KEY 0x003A +KEY 0x0031 +KEY 0x007A +KEY 0x0078 +KEY 0x0063 +KEY 0x0076 +KEY 0x0060 +KEY 0x0061 +KEY 0x0062 +KEY 0x0064 +KEY 0x0065 +KEY 0x006D +KEY 0x0047 +KEY 0x006B +KEY 0x006B +KEY 0x0059 +KEY 0x005B +KEY 0x005C +KEY 0x004E +KEY 0x0056 +KEY 0x0057 +KEY 0x0058 +KEY 0x0045 +KEY 0x0053 +KEY 0x0054 +KEY 0x0055 +KEY 0x0052 +KEY 0x0041 +KEY 0x000A +KEY 0x0067 +KEY 0x006F +KEY 0x004C +KEY 0x003E +KEY 0x003E +KEY 0x004B +KEY 0x0069 +KEY 0x003D +KEY 0x003D +KEY 0x0071 +KEY 0x0073 +KEY 0x007E +KEY 0x0074 +KEY 0x007B +KEY 0x007C +KEY 0x0077 +KEY 0x007D +KEY 0x0079 +KEY 0x0072 +KEY 0x0075 +KEY 0x0069 +KEY 0x0047 +KEY 0x0036 +KEY 0x0036 +KEY 0x0037 +KEY 0x0037 +KEY 0x0037 +KEY 0x0037 +KEY 0x0036 +KEY 0x0036 +KEY 0x006E +KEY 0x0047 \ No newline at end of file diff --git a/src/macos/mod.rs b/src/macos/mod.rs index 557bf53..a47a550 100644 --- a/src/macos/mod.rs +++ b/src/macos/mod.rs @@ -9,6 +9,7 @@ use std::process::exit; use getopts::Options; use std::fs::File; +use std::io::Write; pub mod codes; use codes::*; @@ -48,9 +49,16 @@ impl KeyEvent for InputEvent { //self.event.to_owned().get_integer_value_field(EventField::KEYBOARD_EVENT_KEYCODE) as CGKeyCode unsafe { CGEventGetIntegerValueField(self.event, kCGKeyboardEventKeycode) } } - +/* + fn set_code(&self, code: CGKeyCode) { + //1 + //self.event.to_owned().get_integer_value_field(EventField::KEYBOARD_EVENT_KEYCODE) as CGKeyCode + unsafe { CGEventSetIntegerValueField(self.event, kCGKeyboardEventKeycode) } + } +*/ fn value(&self) -> KeyState { match self.event_type { + kCGEventFlagsChanged => KeyState::DOWN, kCGEventKeyDown => KeyState::DOWN, kCGEventKeyUp => KeyState::UP, kCGEventTapDisabledByTimeout => { @@ -150,7 +158,9 @@ pub fn main_res() -> Result<()> { let key_maps = MacOSKeyMaps::from_cfg(&key_map, &config.config_file); let mask = CGEventMaskBit(kCGEventKeyDown) - | CGEventMaskBit(kCGEventKeyUp); + | CGEventMaskBit(kCGEventKeyUp) + | CGEventMaskBit(kCGEventFlagsChanged) + ; unsafe { let options = 0; @@ -184,45 +194,6 @@ pub fn main_res() -> Result<()> { CFRunLoopRun(); } - unsafe { - /* - CFMachPortRef eventTap; - CGEventMask eventMask; - CFRunLoopSourceRef runLoopSource; - - - // Create an event tap. We are interested in key presses. - let eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp)); - let eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, - eventMask, myCGEventCallback, NULL); - - */ - /* - if (!eventTap) { - fprintf(stderr, "failed to create event tap\n"); - exit(1); - } - - // Create a run loop source. - let runLoopSource = CFMachPortCreateRunLoopSource( - kCFAllocatorDefault, eventTap, 0); - - // Add to the current run loop. - CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, - kCFRunLoopCommonModes); - - // Enable the event tap. - CGEventTapEnable(eventTap, true); - - // Set it all running. - println!("woo1"); - CFRunLoopRun(); - println!("woo2"); - */ - } - - //std::thread::sleep(std::time::Duration::from_millis(400000)); - Ok(()) } @@ -254,13 +225,13 @@ fn parse_args() -> Config { } let args: Vec<_> = env::args().collect(); - + let mut default_configs = Vec::new(); get_env_push("USERPROFILE", "\\keymap.toml", &mut default_configs); get_env_push("APPDATA", "\\keymap.toml", &mut default_configs); - + default_configs.push("keymap.toml".to_string()); - + let c_msg = format!("specify the keymap config file to use (default in order: {:?})", default_configs); let mut opts = Options::new(); @@ -304,6 +275,7 @@ fn parse_args() -> Config { } use libc; +use core_graphics::event::CGEventType::Null; // Opaque Pointer Types pub type Pointer = *mut libc::c_void; @@ -317,6 +289,7 @@ pub type CGEventTapLocation = u32; pub type CGEventTapOptions = u32; pub type CGEventTapPlacement = u32; pub type CGEventType = u32; +pub type CGEventFlags = u64; //pub type CGKeyCode = u16; // Callback Type @@ -434,8 +407,39 @@ pub const kCGEventTapDisabledByTimeout: CGEventType = 0xFFFFFFFE; ); pub fn CGEventTapEnable(port: CFMachPortRef, enable: bool); + + pub fn CGEventGetType(event: CGEventRef) -> CGEventType; + pub fn CGEventGetFlags(event: CGEventRef) -> CGEventFlags; } +const CGEventFlagNull: u64 = 0; + +// Device-independent modifier key bits. +const CGEventFlagAlphaShift: u64 = 0x00010000; +const CGEventFlagShift: u64 = 0x00020000; +const CGEventFlagControl: u64 = 0x00040000; +const CGEventFlagAlternate: u64 = 0x00080000; +const CGEventFlagCommand: u64 = 0x00100000; + +// Special key identifiers. +const CGEventFlagHelp: u64 = 0x00400000; +const CGEventFlagSecondaryFn: u64 = 0x00800000; + +// Identifies key events from numeric keypad area on extended keyboards. +const CGEventFlagNumericPad: u64 = 0x00200000; + +// Indicates if mouse/pen movement events are not being coalesced +const CGEventFlagNonCoalesced: u64 = 0x00000100; + +const NX_DEVICELCTLKEYMASK: u64 = 0x00000001; +const NX_DEVICELSHIFTKEYMASK: u64 = 0x00000002; +const NX_DEVICERSHIFTKEYMASK: u64 = 0x00000004; +const NX_DEVICELCMDKEYMASK: u64 = 0x00000008; +const NX_DEVICERCMDKEYMASK: u64 = 0x00000010; +const NX_DEVICELALTKEYMASK: u64 = 0x00000020; +const NX_DEVICERALTKEYMASK: u64 = 0x00000040; +const NX_DEVICERCTLKEYMASK: u64 = 0x00002000; + /// This callback will be registered to be invoked from the run loop /// to which the event tap is added as a source. #[no_mangle] @@ -443,6 +447,28 @@ pub const kCGEventTapDisabledByTimeout: CGEventType = 0xFFFFFFFE; pub extern fn callback(proxy: Pointer, event_type: CGEventType, event: CGEventRef, key_maps: &mut MacOSKeyMaps) -> CGEventRef { + /* + println!("+++++++++++++++++++++++++++++++++++++++"); + let event_type2 = unsafe { CGEventGetType(event) }; + println!("event_type2: {}", event_type2); + + + let flags = unsafe { CGEventGetFlags(event) }; + println!("flags: {}", flags); + println!("flags: {:#064b}", flags); + + println!("EventFlagsChanged: {:x} ({} {} {} {} {} {} {} {})\n", + flags, + if (flags & NX_DEVICELCTLKEYMASK) != 0 { "lcontrol" } else { "" }, + if (flags & NX_DEVICERCTLKEYMASK) != 0 { "rcontrol" } else { "" }, + if (flags & NX_DEVICELSHIFTKEYMASK) != 0 { "lshift" } else { "" }, + if (flags & NX_DEVICERSHIFTKEYMASK) != 0 { "rshift" } else { "" }, + if (flags & NX_DEVICELCMDKEYMASK) != 0 { "lcommand" } else { "" }, + if (flags & NX_DEVICERCMDKEYMASK) != 0 { "rcommand" } else { "" }, + if (flags & NX_DEVICELALTKEYMASK) != 0 { "lalt" } else { "" }, + if (flags & NX_DEVICERALTKEYMASK) != 0 { "ralt" } else { "" }, + ); + match event_type { kCGEventKeyDown => println!("key down"), @@ -458,17 +484,29 @@ pub extern fn callback(proxy: Pointer, event_type: CGEventType, event: CGEventRe //return event; }, }; + */ unsafe { let mut input_event = InputEvent { event_type, event, }; + /* println!("got keyCode: {}", input_event.code()); - //Some(input_event.event) + println!("---------------------------------------"); + */ + if input_event.value() == KeyState::DOWN { + let code = input_event.code(); + println!("KEY 0x{:04X}", code); + write_key_file(code).expect("error writing key file"); + } + std::ptr::null_mut() + //input_event.event.set //input_event.event - key_maps.send_event(&mut input_event, &DEVICE).expect("macos shouldn't error...") - .unwrap_or_else(|| std::ptr::null_mut()) // None means return NULL + //Null. + //Some(input_event.event) + //key_maps.send_event(&mut input_event, &DEVICE).expect("macos shouldn't error...") + // .unwrap_or_else(|| std::ptr::null_mut()) // None means return NULL //let keyCode = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode); //println!("got keyCode: {}", keyCode); /* @@ -496,6 +534,12 @@ pub extern fn callback(proxy: Pointer, event_type: CGEventType, event: CGEventRe //event } +fn write_key_file(code: CGKeyCode) -> std::io::Result<()> { + let mut buffer = File::create("/Users/mopar/key.txt")?; + write!(buffer, "0x{:04X}", code)?; + Ok(()) +} + /// Redefine macro for bitshifting from header as function here pub fn CGEventMaskBit(eventType: u32) -> CGEventMask { 1 << (eventType)