macos – Swift – 从NSViewController捕获keydown

我想在我的小应用程序中捕获关键事件。

我做了什么:

class ViewController : NSViewController {
...
  override func keyDown(theEvent: NSEvent) {
        if theEvent.keyCode == 124 {
            println("abc")
        } else {
            println("abcd")
        }
    }

    override var acceptsFirstResponder: Bool {
        return true
    }

    override func becomeFirstResponder() -> Bool {
        return true
    }

    override func resignFirstResponder() -> Bool {
        return true
    }

...
}

怎么了:

按下某个键时,会播放Funk音效。

我看过很多帖子都在谈论这是一个属于NSView的委托,NSViewController没有访问权限。但是keydown函数覆盖自动完成类NSViewController类导致我认为这是错误的。

Xcode 8.2.1•Swift 3.0.2
import Cocoa

class ViewController: NSViewController {

    @IBOutlet var textField: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) {
            self.flagsChanged(with: $0)
            return $0
        }
        NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
            self.keyDown(with: $0)
            return $0
        }
    }
    override func keyDown(with event: NSEvent) {
        switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
        case [.command] where event.characters == "l",[.command,.shift] where event.characters == "l":
            print("command-l or command-shift-l")
        default:
            break
        }
        textField.stringValue = "key = " + (event.charactersIgnoringModifiers
            ?? "")
        textField.stringValue += "\ncharacter = " + (event.characters ?? "")
    }
    override func flagsChanged(with event: NSEvent) {
        switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
        case [.shift]:
            print("shift key is pressed")
        case [.control]:
            print("control key is pressed")
        case [.option] :
            print("option key is pressed")
        case [.command]:
            print("Command key is pressed")
        case [.control,.shift]:
            print("control-shift keys are pressed")
        case [.option,.shift]:
            print("option-shift keys are pressed")
        case [.command,.shift]:
            print("command-shift keys are pressed")
        case [.control,.option]:
            print("control-option keys are pressed")
        case [.control,.command]:
            print("control-command keys are pressed")
        case [.option,.command]:
            print("option-command keys are pressed")
        case [.shift,.control,.option]:
            print("shift-control-option keys are pressed")
        case [.shift,.command]:
            print("shift-control-command keys are pressed")
        case [.control,.option,.command]:
            print("control-option-command keys are pressed")
        case [.shift,.command,.option]:
            print("shift-command-option keys are pressed")
        case [.shift,.command]:
            print("shift-control-option-command keys are pressed")
        default:
            print("no modifier keys are pressed")
        }
    }
}

要在按下字符键时摆脱呜呜声,需要对视图进行子类化,重写方法performKeyEquivalent并返回true。

import Cocoa

class View: NSView {
    override func performKeyEquivalent(with event: NSEvent) -> Bool {
        return true
    }
}

Sample Project

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...