7

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.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
vcirilli
  • 321
  • 3
  • 13

8 Answers8

19

check [NSEvent modifierFlags]...

if ([NSEvent modifierFlags ] & NSAlternateKeyMask)
{
    //do something
}
Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • 1
    This was indeed true and a great approach up to Monterey, but I'm seeing modifierFlags always be 0 on Ventura; details [on a sample project I provided today to Apple for FB11723708](https://github.com/cdf1982/ModifierFlagsChecker). – cdf1982 Oct 27 '22 at 15:23
  • 1
    pretty interesting @cdf1982 ... just wondering if you check the even being passed into the action rather than NSEvent.currentEvent if it is different... I don't have Ventura on anything yet so I can't really poke around... – Grady Player Oct 27 '22 at 15:45
  • 1
    I didn't think of checking the event passed into `mouseDown:`, thank you @GradyPlayer! Sadly, it's the same event as `NSApp.currentEvent`, but it was worth a try. I'm exploring, with a bit of repulsion, the idea of importing Carbon and then use `GetCurrentKeyModifiers()`, as that method is returning "512" each time Shift is down, which is what I'm trying to check. Adding a Carbon import at the end of 2022 sounds less than ideal, but for a temporary fix... – cdf1982 Oct 28 '22 at 04:31
  • On macOS Monterey 12.6.1 (21G215) I have found a workaround: just add !0 and it works again. BOOL altKeyPressed = ([event modifierFlags ] & NSEventModifierFlagOption) != 0; – Leonardo Nov 02 '22 at 23:09
5

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.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
user2626974
  • 51
  • 1
  • 3
1

Update: NSAlternateKeyMask is now deprecated. Use NSEventModifierFlagOption instead.

See NSEventModifierFlags for a complete overview about all modifier flags.

fbitterlich
  • 882
  • 7
  • 24
1

Swift 5 - Also helps distinguish between the "up" and "down" presses of Option key

override func flagsChanged(with event: NSEvent) {
    print(event)
    
    if event.keyCode == 58 && event.modifierFlags.contains(NSEvent.ModifierFlags.option){
        print("Option Key is down!")
    }
    else if(event.keyCode == 58)
    {
        print("Option Key is up!")
    }
}
user3064009
  • 2,366
  • 1
  • 12
  • 8
0

It's for someone who using swift and struggling with this.

if NSEvent.modifierFlags.rawValue & NSEvent.ModifierFlags.command.rawValue != 0 {
    // to do something.
}
JZAU
  • 3,550
  • 31
  • 37
0

Swift 2.2:

if NSEvent.modifierFlags().contains(.AlternateKeyMask) {
    print("Option key pressed")
}
Klaas
  • 22,394
  • 11
  • 96
  • 107
0

On macOS 10.14, when called from a NSGestureRecognizerDelegate method in ObjC, [NSEvent modifierFlags] was always returning 0 for me, regardless of what keys were down. I was able to reliably work around this by using [NSApp currentEvent] instead of NSEvent.

- (BOOL)isOptionKeyDown {
    NSEventModifierFlags keyEventFlags = [[NSApp currentEvent] modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
    return (keyEventFlags & NSEventModifierFlagOption) != 0;
}
Aaron T
  • 159
  • 1
  • 6
-1

Swift 4:

func optionKeyPressed() -> Bool
{        
    let optionKey = NSEvent.modifierFlags.contains(NSEvent.ModifierFlags.option)
    return optionKeyPressed
}

if optionKeyPressed()
{
    print ("option key pressed")
}
iphaaw
  • 6,764
  • 11
  • 58
  • 83