I need to respond to keys being pressed on a physical keyboard, but the method pressesBegan
is not being called on any class. I've tried putting it inside my SKScene and my ViewController, but it just is not getting recognized
UPDATE Here is the code, I've replaced the logic with fatalError()
, so that the book will crash when pressesBegan
gets called. Since it's not crashing, pressesBegan
is not being called
The rest of the code has nothing related to responders
import Foundation
import SpriteKit
import SceneKit
import UIKit
public class GameScene: SKScene {
// ... some code
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
fatalError()
}
/// Some more code...
}
class GameViewController: UIViewController {
override func loadView(){
self.view = sceneView
}
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 1920, height: 1080))
override func viewDidLoad() {
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
}
}
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
fatalError()
}
}
let viewController = GameViewController()
viewController.view.becomeFirstResponder()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true
Solved by adding override var canBecomeFirstResponder { true }
to the UIViewController
, calling myViewController.becomeFirstResponder()
after presenting the controller and calling super.pressesBegan
when I'm not handling the press