flagsChanged allows to test if some modifier key is pressed ; for instance:
You can declare global var in your controller :
var isOptionKeyPressed = false
var isControlKeyPressed = false
Then set them through flagsChanged
override func flagsChanged(with theEvent: NSEvent) {
isOptionKeyPressed = theEvent.modifierFlags.contains(NSEvent.ModifierFlags.option)
isControlKeyPressed = theEvent.modifierFlags.contains(NSEvent.ModifierFlags.control
// You can do what you need when key is pressed, such as change the keyboard layout
super.flagsChanged(with: theEvent)
}