In Mac OS X 10.6, NSEvent
has a +modifierFlags
class method to determind the currently pressed modifier flags. In 10.5, using [[NSApp currentEvent] modifierFlags]
only updates after mouse move. Is there any way to asynchronously get the modifier flags?
views:
49answers:
2
+2
A:
GetCurrentKeyModifiers()
. It's a C function but it's available in Cocoa applications (it's part of the Carbon framework, but is still available on 64-bit systems, IIRC.) Note that the flags it uses are not equal to the Cocoa flags, so your tests will have to change appropriately.
Jonathan Grynspan
2010-09-07 16:13:37
Is it only available on 64-bit systems? I'm trying to target 32-bit systems running 10.5.
Alexsander Akers
2010-09-07 16:40:56
Alexsander Akers: No. It has been available ever since the days of CarbonLib on Mac OS, including on all the 32-bit systems between then and now; he mentioned 64-bit compatibility because a lot of people, upon seeing a Carbon function suggested, will pounce and scream “Carbon's not available in 64-bit!”.
Peter Hosey
2010-09-07 17:25:47
+1
A:
According to a comment on this blog post, you can use CGEventCreate() and CGEventGetFlags().
CGEventRef event = CGEventCreate(NULL /*default event source*/);
CGEventFlags mods = CGEventGetFlags(event);
if (mods & kCGEventFlagMaskShift)
NSLog(@"Shift key is being pressed");
The modifier flags returned by CGEventGetFlags() (CGEventFlags) are the same as the NSEvent ones.
David Gelhar
2010-09-07 16:24:23