You are not using the returned UIKeyCommand instance up
.
Apple: "After creating a key command object, you can add it to a view controller using the addKeyCommand: method of the view controller. You can also override any responder class and return the key command directly from the responder’s keyCommands property."
class Test: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
setKeys()
}
func setKeys() {
let up = UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(upPressed))
self.addKeyCommand(up)
}
@objc func upPressed() {
print("Hello")
}
}
Tested this using a simulator and hardware keyboard.
ADDITIONALLY: If you are going to implement it through the UIView directly you have to do : "...You can also override any responder class and return the key command directly from the responder’s keyCommands property." since UIView conforms to UIResponder
class CustomView: UIView{
override var keyCommands: [UIKeyCommand]? {
return [UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(upPressed))]
}
@objc func upPressed(){
print("hello world")
}
}