0

I have written this code. My problem is, the keyDown function only listens to certain keys (letters, numbers, space, characters, Tab.). However, it completely ignores keys like caps lock and ⇧ Shift and Control. How can I make it listen to these keys?

class GameClient: NSView {
    override var acceptsFirstResponder: Bool {
        return true
    }
    override func keyDown(with event: NSEvent) {
        print(event.keyCode)
    }
    override func keyUp(with event: NSEvent) {

    }
}

The answers in the suggested dupe don't fully answer my question. I don't want to see if ⇧ Shift was being pressed while I was pressing A, I want to detect when ⇧ Shift alone is pressed, for example.

1 Answers1

1

The question is how to capture a standalone shift key event, rather than detecting if shift is being pressed while another key is. You want to do this:

override func flagsChanged(with event: NSEvent) {
    print(event.keyCode)
}
override func keyDown(with event: NSEvent) {
    print(event.keyCode)
}
override func keyUp(with event: NSEvent) {

}
matt
  • 515,959
  • 87
  • 875
  • 1,141