Keyboard layout

Hi, Tell me which method is responsible for pressing the function keys. func keyUp and keyDown as I understand it is not appropriate here. I need when you press the key combination switch the keyboard layout run my method.

Accepted Reply

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)
    }

Replies

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)
    }

Thank you