使用Swift处理AppKit中的键盘事件

BJ *_*mer 11 appkit swift

我有一个自定义的NSView子类,需要处理一些键盘事件.在Objective-C中,我可能会这样处理它:

-(void)keyDown:(NSEvent *)event
{
    unichar ch = [[event charactersIgnoringModifiers] characterAtIndex:0];

    if (ch == NSUpArrowFunctionKey && (event.modifierFlags & NSCommandKeyMask)) {
        // Scroll to top
        return;
    }
    else if (ch == NSDownArrowFunctionKey && (event.modifierFlags & NSCommandKeyMask)) {
        // Scroll to bottom
        return;
    }

    switch (ch) {
        case NSRightArrowFunctionKey:
            // Select the current row
            return;
        case ' ':
            // Scroll down one page
            return;
        default:
            break;
    }

    [super keyDown:event];
}
Run Code Online (Sandbox Code Playgroud)

但是,在Swift中,characterAtIndex:返回a unichar,while NSUpArrowFunctionKey: Int" ": String(或Character).我不清楚如何将unichar转换为StringCharacter.

我得到了这个工作,但感觉就像一个丑陋的解决方法.有没有更好的办法?

func keyDown(theEvent: NSEvent) {
    let char = Int(theEvent.charactersIgnoringModifiers.utf16[0])  // <----- This seems ugly
    let hasCommand = (theEvent.modifierFlags & .CommandKeyMask).value != 0

    switch char {

        case NSUpArrowFunctionKey where hasCommand == true:
            // Scroll to top
            break

        case NSDownArrowFunctionKey where hasCommand == true:
            // Scroll to bottom
            break

        case NSRightArrowFunctionKey where hasCommand == true:
            // Select the current row
            break

        case Int(" ".utf16[0]):   //  <---- Surely there's a better way of doing this?
            // Scroll down one page
            break

        default:
            super.keyDown(theEvent)
    }
}
Run Code Online (Sandbox Code Playgroud)

Ada*_*ble 14

让经常被忽视interpretKeyEvents()的人为你做一些麻烦的事.它知道各种键,包括箭头键:

override func keyDown(event: NSEvent) {
    interpretKeyEvents([event]) // calls insertText(_:), moveUp(_:), etc.
}

override func insertText(insertString: AnyObject) {
    let str = insertString as! String
    switch str {
    case " ":
        println("User hit the spacebar.")
    default:
        println("Unrecognized input: \(str)")
    }
}

override func moveUp(sender: AnyObject?) {
    println("Up arrow.")
}

override func moveLeft(sender: AnyObject?) {
    println("Left arrow.")
}

override func deleteBackward(sender: AnyObject?) {
    println("Delete.")
}
Run Code Online (Sandbox Code Playgroud)

NSResponder类类参考部分应诉消息列出了这些和其它方法来处理键盘事件.


NSA*_*ict 7

为什么不使用扩展

extension NSEvent {

    var character: Int {
        // Note that you could also use Int(keyCode)
        return Int(charactersIgnoringModifiers.utf16[0])
    }

}
Run Code Online (Sandbox Code Playgroud)
override func keyDown(theEvent: NSEvent!) {
    switch theEvent.character {
        case NSUpArrowFunctionKey:
            println("up!")
        case 0x20:
            println("spacebar!")
        default:
            super.mouseDown(theEvent)
    }
}
Run Code Online (Sandbox Code Playgroud)

另请注意,如回答所述,没有公共枚举定义每个密钥的所有密钥代码.最简单的方法是使用a测试它的值println(),然后在switch语句中使用该值.


编辑

或者你也可以扩展Character类

import Foundation
extension Character {

    var keyCode: Int {
        return Int(String(self).utf16[String.UTF16View.Index(0)])
    }

}
Run Code Online (Sandbox Code Playgroud)

并像这样测试它

case Character(" ").keyCode: // Spacebar
    println("spacebar!")
Run Code Online (Sandbox Code Playgroud)


Eon*_*nil 5

OSX 使用至少两个不同的层来处理按键事件。

  • keyCode。我相信这是映射到每个硬件键盘键的代码。
  • 统一码。映射到语义虚拟键的预定义 Unicode 代码点。

您需要使用 Unicode 密钥来正确处理用户事件。您可以像这样从事件对象中获取 Unicode 代码点。

override func keyDown(theEvent: NSEvent) {
    let s   =   theEvent.charactersIgnoringModifiers!
    let s1  =   s.unicodeScalars
    let s2  =   s1[s1.startIndex].value
    let s3  =   Int(s2)
    switch s3 {
    case NSUpArrowFunctionKey:
        wc1.navigateUp()
        return
    case NSDownArrowFunctionKey:
        wc1.navigateDown()
        return
    default:
        break
    }
    super.keyDown(theEvent)
}
Run Code Online (Sandbox Code Playgroud)

请注意,它unicodeScalars不是随机访问的,因此您需要使用显式索引对象 --- startIndex