As an experiment, I am trying to achieve the following:
- Let spacebar work as a modifier key - like the Shift key - where holding the spacebar key down and typing keys print different letters. Releasing the spacebar would set the state back to normal, and just pressing it behaves like a normal space key.
I was thinking of handling the keydown and keyup event, but apparently handleEvent:client: in IMKServerInput Protocol seems to only catch key down and mouse events.
Without much experience with cocoa, I’ve tried some methods with no success:
- went through the Technical Note 2128 via internet archive, which gave me the suitable explanations of plist items. Still, nothing about keyup.
- tried adding NSKeyUpMask to
recognizedEvents:in IMKStateSetting Protocol, but that didn’t seem to catch the event either. - tested a bit with addLocalMonitorForEventsMatchingMask:handler: but nothing happens.
- failed to find a way to make
NSFlagsChangedevent fire with spacebar. - read about Quartz Event Service and
CGEventTapwhich seems to handle user inputs in lower level. Didn’t go further to this route, yet. - IOHIDManager?
I reached to a conclusion that IMKit is only capable of passively receiving events.
Since it is not an application, there is no keyUp: method to override - AFAIK, IMKit does not inherit NSResponder class.
Unfortunately cocoa is way too broad and has much less (or overflowed with non-helping) documentations for a novice like me to dive in.
Can anyone help me to the right direction?
I tried all possible alternatives one by one, and eventually achieved it by creating a global EventTap with
CGEventTap.The code basically looks like this:
where
myCGEventCallbackhandles the global states.Meanwhile here are some of what I've found out:
IMKServerInput'catch' the NSKeyUp event. Just adding anNSKeyUpMasktorecognizedEvents:would not work.addLocalMonitorForEventsMatchingMask:handler:andCGEventTapCreateForPSNwould not catch the event. I suppose this is because though an Input Method may run as a separate process, the event itself is fired from the application, like TextEdit, and handed over to the Input `Method.IOHIDManager:is for adding new hardware devices and making drivers./Library/Input Methodsdoes not run with sudo privilege --, or registering the application to the Accessibility control. That's in System Preferences → Security & Privacy → Privacy tab → Accessibility, in Mavericks.