my problem. I'm working with a NSEvent, which happens to have a var modifierFlags of type NSEventModifierFlags.
I want to check whether the user had a modifier key pressed (Command), so basically I want to check if CommandKeyMask is on.
What's the best/right way to do this in Swift ?
The following works in Xcode7-beta3, Swift 2
In Swift 2, bit field style enums like NSEventModifierFlags
have been updated to conform to the OptionSetType
protocol.
If you want to check if an option set contains a specific option, you no longer need to use bitwise &
and a nil check. You can simply ask the option set if it contains a specific value in the same way that you would check if an array contained a value.
if theEvent.modifierFlags.contains(.CommandKeyMask) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With