0
我删除@UIApplicationMain
中的AppDelegate.swift
并写下如下的main.swift
,但程序仍然无法启动keyPressed
函数。但是每个keydown都会执行print("send event2")
。如何拦截swift iOS应用程序的关键事件?
如何启动keyPressed
函数?这个程序有问题吗?
import UIKit
import Foundation
class TApplication: UIApplication {
override func sendEvent(_ event: UIEvent!) {
super.sendEvent(event)
}
override var keyCommands: [UIKeyCommand]? {
print("send event2") // this is an example
return [
UIKeyCommand(input: "1", modifierFlags: [], action: Selector(("keyPressed:")), discoverabilityTitle: “1”),
UIKeyCommand(input: "2", modifierFlags: .shift, action: Selector("keyPressed:"), discoverabilityTitle: “2”)]
]
}
func keyPressed(sender: UIKeyCommand) {
print("keypressed event !!!!!!!!") // this is an example
}
}
“行动:#selector(的keyPressed(发件人:))” 而不是“操作:选择器((“keyPressed:”))“ – baileymiller2017