在Swift中检测按键事件 [英] Detecting key press event in Swift

查看:112
本文介绍了在Swift中检测按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找一种方法来检测Swift上是否已按下(键盘上的)一个键.任何想法和建议将不胜感激.

I'm trying to find a way to detect if a key (on a keyboard) has been pressed on Swift. Any ideas and suggestions will be greatly appreciated.

推荐答案

由于您更新了问题,并且想知道如何在窗口中执行此操作,因此这里提供了答案.子类化NSWindow并改用该子类.

Since you updated your question and you wanted to know how to do this for a window, here's an answer. Subclass NSWindow and use this subclass instead.

您的自定义类应如下所示:

Your custom class should look like this:

import Cocoa

class EditorWindow: NSWindow {
    override func keyDown(event: NSEvent) {
        super.keyDown(event)
        Swift.print("Caught a key down: \(event.keyCode)!")
    }
}

如果您已在Interface Builder/XCode中创建了窗口,请单击窗口对象,然后转到属性检查器( + + 3 ).属性检查器将在右侧的侧栏中.确保在Interface Builder中选择了您的窗口,并在Custom Class区域中的属性"检查器的顶部,将新类放入类输入中.

If you've made your window in Interface Builder/XCode, click the window object and go to the Attribute Inspector (++3). The Attribute Inspector will be in the sidebar on the right. Making sure your window is selected in Interface Builder, at the top of the Attribute Inspector in the Custom Class area put your new class in the class input.

为了将事件从此窗口类传达到我的应用程序,我在窗口中添加了一个函数,该函数接受一个回调函数,然后将其存储在回调函数数组中.我可以通过AppDelegate访问此窗口,该窗口可以获取对当前主窗口的弱引用.然后在上面的函数中,我对整个回调进行迭代,并以NSEvent作为参数对其进行调用.我还首先检查是否通过事件的modifierFlags属性首先按下了诸如选项键之类的命令键.最终看起来像这样:

In order to communicate the event from the this window class to my app I add a function to the window that accepts a callback function that I then store in an array of callback functions. I get access to this window through the AppDelegate which can get a weak reference to the current main window. Then in the above function I iterate overall the callbacks and call it with the NSEvent as the argument. I also first check to see if any command keys like the option keys are being pressed first through modifierFlags property on the event. It ends up looking like this:

import Cocoa

typealias Callback = (NSEvent) -> ()

class KeyCaptureWindow: NSWindow {

    var keyEventListeners = Array<Callback>()

    override func keyDown(event: NSEvent) {
        if event.modifierFlags.contains(NSEventModifierFlags.CommandKeyMask) {
            super.keyDown(event)
            return
        }
        for callback in keyEventListeners {
            callback(event)
        }
    }

    func addKeyEventCallback(callback: Callback) {
        keyEventListeners.append(callback)
    }


}

然后在代码的其他地方,我有这样一行:

And then elsewhere in my code I have a line like so:

    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
    let mainWindow = appDelegate.getWindow()
    mainWindow.addKeyEventCallback(handleKeyEvent)

我在我的应用程序委托类中添加了getWindow方法.此方法将NSWindow强制类型转换为KeyCaptureWindow.也许有更好的方法来完成所有这些工作,但这对我有用.另一种可能的方法是使用急救人员和NSView,但这不是我一直在做的方式.

I added the getWindow method to my app delegate class. This method returns the NSWindow cast to KeyCaptureWindow. There may be a better way to do all this but this works for me. Another way to possibly do this is to use first responders and NSView, but that's not how I've been doing it.

这篇关于在Swift中检测按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆