I'm looking for application-wide access to raw keyboard events in OS X, either using the Cocoa or Carbon frameworks (or any of the underlying APIs, for that matter). I know that I can override NSApplication's sendEvent: to get raw keyboard information, but for the meta keys (command, control, alternate, shift, etc) don't show up as keystroke events. I'm looking for something analogous to Microsoft's DirectInput framework.
Thanks!
Turn on Full Keyboard Access On your Mac, choose Apple menu > System Preferences, click Accessibility , click Keyboard, then click Navigation. Select the Enable Full Keyboard Access checkbox.
Use your keyboard like a mouseChoose Apple menu > System Preferences, then click Keyboard. Click Shortcuts. From the bottom of the preferences window, select the checkbox “Use keyboard navigation to move focus between controls.” (In macOS Mojave or earlier, this setting appears as an “All controls” button.)
On your Mac, click the Input menu in the menu bar, then choose Show Keyboard Viewer. If the command isn't shown, choose Apple menu > System Preferences, click Keyboard , click Input Sources, then select “Show Input menu in menu bar.”
The Input menu appears in the right side of the menu bar. It contains enabled input sources as well as easy access to the Character Viewer and the Keyboard Viewer, and offers a fast way to switch between them. This option is automatically selected when you add input sources.
I think the equivalent to DirectInput is HID Manager. HID stands for "human interface device" and HID Manager (sometimes called HIDLib) is the low-level API to HIDs: keyboards, mice, and joysticks.
Leopard's got a new HID Manager API, documented in Technical Note TN2187. The pre-Leopard API is documented in HID Class Device Interface Guide. I wrote an Objecive-C wrapper around the older APIs, DDHidLib, which you may find useful. The Leopard API is much nicer. I'd use that directly, if you can.
The Core Graphics framework also has some useful functionality buried in it as part of the remote operation system. Look for CGRemoteOperation.h, and check out the Quartz Events reference.
You can use the Quartz Events system to install application-specific or system-wide "event taps", which let you monitor and inject keyboard and mouse events at a pretty low level. A few years ago there were some bugs with application-specific event taps, but they've hopefully been worked out by now.
I think the HID stuff is mostly for driver development, so if you're just looking for a tool for your application, HID is probably overkill.
NSResponder
derived classes have a method -(void)flagsChanged
: that gets called when meta-keys are held down.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With