views:

49

answers:

2

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?

+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
Is it only available on 64-bit systems? I'm trying to target 32-bit systems running 10.5.
Alexsander Akers
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
+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