Problems with keyUp / keyDown in Cocoa

I am programming an application that will be a basic video game, but the keyUp: (NSEvent) and keyDown: (NSEvent) methods are not executed.

This is my code:

-(void)keyUp:(NSEvent*)event {
    NSLog(@"Key %@", event);
}

-(void)keyDown:(NSEvent*)event {   
    switch( [event keyCode] ) {
        case 126:
        case 125:
        case 124: 
        case 123:       
   NSLog(@"Arrow key pressed!");
   break;
        default:
   NSLog(@"Key %@", event);
   break;
    }
}

I made sure that it was in a subclass of NSResponder (although it is in NSOpenGLView, can this affect it?), And the other is that I do not see any problems. Any help is very helpful. Thank:)

+3
source share
1 answer

Have you implemented the NSResponder acceptsFirstResponder method to return YES?

- (BOOL)acceptsFirstResponder {
    return YES;
}
+5
source

Source: https://habr.com/ru/post/1779512/


All Articles