Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct access to keyboard events in OSX

Tags:

macos

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!

like image 330
Stephen Deken Avatar asked Feb 03 '09 13:02

Stephen Deken


People also ask

How do I enable full keyboard access on my Mac?

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.

How do I use keyboard navigation on Mac?

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.)

How do I display keyboard input on Mac screen?

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.”

What is input on Mac?

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.


3 Answers

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.

like image 63
Dave Dribin Avatar answered Sep 29 '22 23:09

Dave Dribin


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.

like image 39
Greg Avatar answered Sep 30 '22 01:09

Greg


NSResponder derived classes have a method -(void)flagsChanged: that gets called when meta-keys are held down.

like image 41
Walter Avatar answered Sep 29 '22 23:09

Walter