macos - Swift - 从 NSViewController 捕获按键

标签 macos swift

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

我做了什么:

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 音效会播放。

我看到很多帖子都在谈论这是一个属于 NSViewNSViewController 没有访问权限的委托(delegate)。但是 keydown 函数 overrideNSViewController 类型的类中自动完成,这让我相信这是错误的。

最佳答案

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, .control, .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, .control, .option, .command]:
            print("shift-control-option-command keys are pressed")
        default:
            print("no modifier keys are pressed")
        }
    }
}

要消除按下字符键时发出的咕噜声,您需要对 View 进行子类化,覆盖方法 performKeyEquivalent 并返回 true。

import Cocoa

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

Sample Project

关于macos - Swift - 从 NSViewController 捕获按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446978/

相关文章:

ios - 如果没有剩余行,PrefetchRowsAt 将不起作用

macos - 适用于 Mac 的 Diff 工具,无需将文本保存到文件

java - JFrame repaint() 和 revalidate() 仅在 Mac 操作系统上调整窗口大小时更新

ios - RealmSwift 初始值设定项 - Xcode 修复它一直出错

ios - Swift 2 - AVAudioPlayer 不播放音频

swift - 移除 Realm 中的列

objective-c - 使用 Cocoa 在另一个应用程序之上创建 HUD

macos - NSWindow 调整大小后 NSView 变形

objective-c - CPU 使用率 OS X 界面

ios - 根据其内容调整 UIScrollView 中 UIView 的大小