When using the flagsChanged(with event: NSEvent)
method of the NSResponder
class, how do I test against the event.modifierFlags
option set for individual modifier keys being pressed/released?
Asked
Active
Viewed 453 times
1

Arcanelab
- 147
- 1
- 2
- 8
-
Possible duplicate of [Checking keyDown event.modifierFlags yields error](https://stackoverflow.com/questions/33158513/checking-keydown-event-modifierflags-yields-error) – Ssswift Jun 08 '17 at 14:33
-
@EricAya I guess the title of my question was not worded properly: I should have asked how to properly test against certain values in `event.modifierFlags` via Swift. In Objective C one must use bitwise masking, but in Swift that's non-idiomatic and should instead use `event.modifierFlags.contains(_ member: NSEventModifierFlags)`, as I later have found out and have pointed out in my answer. This is an example when you need to deal with Cocoa in different ways based on what programming language you use. I hope this helps understanding my motivation behind asking a new question. – Arcanelab Jun 08 '17 at 14:46
-
@Arcanelab Thank you for the explanation. I agree, in this specific case. No problem. – Eric Aya Jun 08 '17 at 14:55
1 Answers
3
I have found a solution, here I check for the shift
key:
override func flagsChanged(with event: NSEvent)
{
if event.modifierFlags.contains(.shift)
{
// shift pressed
}
if event.modifierFlags.contains(.shift) == false
{
// shift released
}
}

Arcanelab
- 147
- 1
- 2
- 8