Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture modifier keys pressing ( Control, Alt, Shift o CMD )

I know how to detect key pressing. All keys except Control, Alt, Shift and CMD.

How can i detect when this keys are being pressed.

Thanks in advance.

like image 656
Jorge Vega Sánchez Avatar asked Apr 05 '11 10:04

Jorge Vega Sánchez


1 Answers

If you want to detect these keys in something like a NSView object, have a look at the NSResponder class. When you overwrite a NSView class (or one of its sublcasses), you can overwrite keyDown:(NSEvent *)theEvent(Apple Documentation). When you call [theEvent modifierFlags], a NSUInteger bitfield is returned, which you can then evaluate.

For instance, with

if ([theEvent modifierFlags] & NSCommandKeyMask) {
   ...
}
you can check if the Command key is pressed.

See Apple's Cocoa Event-handling Guide, especially the section "Handling Key Events" for more information.

like image 136
goetz Avatar answered Sep 19 '22 02:09

goetz