keyDown不起作用 [英] keyDown doesn't work swift
问题描述
override func keyDown(theEvent:NSEvent){
super.keyDown(theEvent)
切换theEvent.character {
case NSLeftArrowFunctionKey:
println(1)
case NSRightArrowFunctionKey:
println(2)
case NSDownArrowFunctionKey:
println(3)
case NSUpArrowFunctionKey:
println(4)
默认值:
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)
标签++
}
x = 0
y + = 105
}
为什么 keyDown
不行,所以我真的很感激任何帮助。
编辑: / strong>
整个类代码:
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()//某些颜色将添加
var pic512 = NSColor()
var pic1024 = NSColor()
var pic2048 = NSColor()
覆盖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)
标签++
}
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
默认值:
ar [i] .stringValue =4
ar [i] .backgroundColor = pic4
}
left -
break
}
tmp ++
}
}
}
覆盖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()
}
覆盖func keyDown(theEvent:NSEvent){
super.keyDown(theEvent)
切换theEvent.character {
case NSLeftArrowFunctionKey:
println(1 )
case NSRightArrowFunctionKey:
println(2)
case NSDownArrowFunctionKey:
println(3)
case NSUpArrowFunctionKey:
println(4)
默认值:
super.mouseDown(theEvent)
}
}
}
你只需要使用一个不可见的按钮。首先在窗口中添加一个方形按钮。为其分配所需的密钥。使您的按钮0像素宽,0像素高。添加一个IBAction到您的按钮和代码将被执行时,用户按键。
另一个选项是addLocalMonitorForEventsMatchingMask更多信息检查此链接
https://stackoverflow.com/a/32447474/2303865
That's my code:
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
}
}
As you can see, I try to recognize if one of arrow buttons was pressed, but it never works. Even the function keyDown never seems to be called.
That's what is written in viewDidAppear
, if this can help you to provide the answer:
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()
}
In viewDidLoad
I just add some non-selectable and non-editable NSTextFields like that:
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
}
I just have no idea why keyDown
doesn't work, so I'll really appreciate any help.
EDIT:
The whole class code:
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)
}
}
}
You just need to use an invisible button. First add a square button to your window. assign the desired key for it. make your button 0 pixels wide and 0 pixels height. Add an IBAction to your button and the code to be executed when the user press the key.
Another option is to addLocalMonitorForEventsMatchingMask for more info check this link
https://stackoverflow.com/a/32447474/2303865
这篇关于keyDown不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!