I am starting to program SpriteKit and trying to figure out how to handle keyboard input.
What I have found so far is that you should subclass NSResponder and implement it as follows:
@interface AppDelegate : NSResponder <NSApplicationDelegate> -(void)keyUp:(NSEvent *)theEvent; -(void)keyDown:(NSEvent *)theEvent; @end @implementation AppDelegate -(void)keyUp:(NSEvent *)theEvent { NSLog(@"Key Released"); } -(void)keyDown:(NSEvent *)theEvent { NSLog(@"Key Pressed"); } @end
Obviously, there are several methods / properties in the AppDelegate interface and implementation, but I did not AppDelegate them there to save the corresponding question.
Then I will start using key codes to determine which keys are pressed, but the keyUp and keyDown methods keyUp not even called. I'm not sure why.
Any help?
Update: Thanks for your answers! I found that you need to implement keyUp and keyDown directly in your scene class, because they will not be called from AppDelegate. Thanks again for your help!
objective-c sprite-kit macos
Dovahkiin
source share