keyDown 不起作用 swift

keyDown doesn't work swift

这是我的代码:

override func keyDown(theEvent: NSEvent) {
    super.keyDown(theEvent)
    switch theEvent.character {
    case NSLeftArrowFunctionKey:
        println(1)
    case NSRightArrowFunctionKey:
        println(2)
    case NSDownArrowFunctionKey:
        println(3)
    case NSUpArrowFunctionKey:
        println(4)
    default:
        break
    }
}

如您所见,我尝试识别是否按下了其中一个箭头按钮,但它始终不起作用。甚至似乎从未调用函数 keyDown。

viewDidAppear里面就是这么写的,如果能帮到你解答的话:

override func viewDidAppear() {
    super.viewDidAppear()
    self.view.window?.styleMask = NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask
    var frame = self.view.window?.frame
    var newHeight = CGFloat(438)
    var newWidth = CGFloat(415)
    frame?.size = NSMakeSize(newWidth, newHeight)
    self.view.window?.setFrame(frame!, display: true)
    self.view.window?.backgroundColor = NSColor.darkGrayColor()
}

viewDidLoad 中,我只是添加了一些不可选择和不可编辑的 NSTextFields,例如:

var x = CGFloat(0)
    var y = CGFloat(0)
    var tag = 1

    for var i = 0; i < 4; i++ {
        for var j = 0; j < 4; j++ {
            var tile = NSTextField(frame: NSRect(x: x, y: y, width: 100, height: 100))
            tile.enabled = false
            tile.stringValue = ""
            tile.font = NSFont(name: "Helvetica", size: 75)
            tile.backgroundColor = NSColor.lightGrayColor()
            tile.editable = false
            tile.selectable = false
            tile.drawsBackground = true
            tile.alignment = NSTextAlignment(rawValue: 2)!
            tile.tag = tag
            x += 105
            ar.append(tile)
            self.view.addSubview(tile)
            tag++
        }
        x = 0
        y += 105
    }

我只是不知道为什么 keyDown 不起作用,所以非常感谢您的帮助。

编辑:

整个class代码:

import Cocoa
import AppKit
import Foundation

class _048Main: NSViewController {

var ar : Array<NSTextField> = []
var left = 0

var dict : Dictionary<Int, NSColor> = [:]

var pic2 = NSColor.redColor()
var pic4 = NSColor.whiteColor()
var pic8 = NSColor.orangeColor()
var pic16 = NSColor.magentaColor()
var pic32 = NSColor()
var pic64 = NSColor()
var pic128 = NSColor()
var pic256 = NSColor() // Some colors will be added
var pic512 = NSColor()
var pic1024 = NSColor()
var pic2048 = NSColor()


override func viewDidLoad() {
    super.viewDidLoad()

    dict[2] = pic2
    dict[4] = pic4
    dict[8] = pic8
    dict[16] = pic16
    dict[32] = pic32
    dict[64] = pic64
    dict[128] = pic128
    dict[256] = pic256
    dict[512] = pic512
    dict[1024] = pic1024
    dict[2048] = pic2048

    var x = CGFloat(0)
    var y = CGFloat(0)
    var tag = 1

    for var i = 0; i < 4; i++ {
        for var j = 0; j < 4; j++ {
            var tile = NSTextField(frame: NSRect(x: x, y: y, width: 100, height: 100))
            tile.enabled = false
            tile.stringValue = ""
            tile.font = NSFont(name: "Helvetica", size: 75)
            tile.backgroundColor = NSColor.lightGrayColor()
            tile.editable = false
            tile.selectable = false
            tile.drawsBackground = true
            tile.alignment = NSTextAlignment(rawValue: 2)!
            tile.tag = tag
            x += 105
            ar.append(tile)
            self.view.addSubview(tile)
            tag++
        }
        x = 0
        y += 105
    }
    left = 16
    generateTile()
    generateTile()
}

func generateTile() {
    var r = Int(arc4random_uniform(UInt32(left)))
    var tmp = 0
    for var i = 0; i < 16; i++ {
        if ar[i].stringValue == "" {
            if tmp == r {
                var t = Int(arc4random_uniform(10))
                switch t {
                case 0...1:
                    ar[i].stringValue = "2"
                    ar[i].backgroundColor = pic2
                default:
                    ar[i].stringValue = "4"
                    ar[i].backgroundColor = pic4
                }
                left--
                break
            }
            tmp++
        }
    }
}

override func viewDidAppear() {
    super.viewDidAppear()
    self.view.window?.styleMask = NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask
    var frame = self.view.window?.frame
    var newHeight = CGFloat(438)
    var newWidth = CGFloat(415)
    frame?.size = NSMakeSize(newWidth, newHeight)
    self.view.window?.setFrame(frame!, display: true)
    self.view.window?.backgroundColor = NSColor.darkGrayColor()
}


override func keyDown(theEvent: NSEvent) {
    super.keyDown(theEvent)
    switch theEvent.character {
    case NSLeftArrowFunctionKey:
        println(1)
    case NSRightArrowFunctionKey:
        println(2)
    case NSDownArrowFunctionKey:
        println(3)
    case NSUpArrowFunctionKey:
        println(4)
    default:
        super.mouseDown(theEvent)
    }
}

}

您的应用程序甚至可以构建吗?

NSEvent 个实例没有 character 属性,它们有 characters 属性。使用 character 应该会导致构建失败和 Xcode 的错误消息。更重要的是 characters returns 一个可选的 String 而你的 switch 案例是类型 Int - 这种类型不匹配也会导致构建失败。

根据我的经验,找出是否按下了任何箭头键的最简单方法是打开事件的 keyCode 属性。

import Foundation
import Cocoa

let kLeftArrowKeyCode:  UInt16  = 123
let kRightArrowKeyCode: UInt16  = 124
let kDownArrowKeyCode:  UInt16  = 125
let kUpArrowKeyCode:    UInt16  = 126


class ViewController: NSViewController {

    override func keyDown(theEvent: NSEvent) {

        switch theEvent.keyCode {

        case kLeftArrowKeyCode:
            println(1)
        case kRightArrowKeyCode:
            println(2)
        case kDownArrowKeyCode:
            println(3)
        case kUpArrowKeyCode:
            println(4)
        default:
            break
        }
    }   
}

如果您愿意,您仍然可以使用预定义常量,但这些代表 Unicode 代码,因此您需要先将它们转换为字符串,然后再打开 characters

你只需要使用一个不可见的按钮。首先在 window 中添加一个方形按钮。为其分配所需的键。使您的按钮宽度为 0 像素,高度为 0 像素。将 IBAction 添加到您的按钮和用户按下该键时要执行的代码。

另一种选择是添加 LocalMonitorForEventsMatchingMask 以获取更多信息,请查看此 link

试试这个代码

override func viewDidLoad()
{
    super.viewDidLoad()

    NSEvent.addLocalMonitorForEventsMatchingMask(NSEventMask.KeyDownMask, handler: myKeyDownEvent)
}

func myKeyDownEvent(event: NSEvent) -> NSEvent
{
    print(event.keyCode)
    // Do stuff with keyCode. In my own projects I trap certain
    // keycodes.
    // If you want to block certain keycodes just change the return value
    // to an optional ->NSEvent? and return nil instead of event.
    return event
}

忽略关于转换字符等的额外内容,如果您的 keyDown() 函数甚至没有被调用,最可能的原因是您没有告诉 OS 您的视图可以接受按键事件(通过覆盖 acceptsFirstResponder)。

在视图中执行此操作 class:

override var acceptsFirstResponder: Bool { get { return true } }