简体   繁体   中英

Why keyDown method does not work in Cocoa macOS?

I want use keyDown method to see which key pressed in keyboard but it does not work also my computer makes sounds to tell the key even does not work.

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
  
    }
 
    override func keyDown(with event: NSEvent) {
        print(event)
    }

}

Only the responders in the current responder chain get a shot at handling events like key events, and then only if some other responder in the chain doesn't handle it first. So, you'll need to make sure that your view controller is managing a view that's the keyboard focus when the event actually takes place, and that the focused view doesn't handle the event itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM