Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Option key is down when user clicks a NSButton

I have a few checkboxes along with textfields on a NSPanel that opens to get user parameters. As an option, I'd like the user to be able to set/unset all the checkboxes on the panel by holding the option key when they press any of the checkboxes.

I'm not sure where/how to check what the key board is doing when the user clicks the button.

like image 734
vcirilli Avatar asked May 13 '13 23:05

vcirilli


3 Answers

check [NSEvent modifierFlags]...

if ([NSEvent modifierFlags ] & NSAlternateKeyMask)
{
    //do something
}
like image 64
Grady Player Avatar answered Jun 21 '23 09:06

Grady Player


Just my 2c, a Swift 3 version:

if NSEvent.modifierFlags().contains(NSEventModifierFlags.command) {
    print("Bingo")
}

One can see the rest of the flags in the documentation for NSEventModifierFlags.

like image 37
user2626974 Avatar answered Jun 21 '23 08:06

user2626974


Update: NSAlternateKeyMask is now deprecated. Use NSEventModifierFlagOption instead.

See NSEventModifierFlags for a complete overview about all modifier flags.

like image 35
fbitterlich Avatar answered Jun 21 '23 08:06

fbitterlich