Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keyUp/keyDown problems in Cocoa

I'm programming an app that will be a basic video game, but the keyUp:(NSEvent ) and keyDown:(NSEvent) methods are failing to execute.

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 it was in a subclass of NSResponder (although it is in NSOpenGLView, could that be affecting it?) and other then that I don't see any problem. Any help is greatly appriciated. Thanks :)

like image 969
Jumhyn Avatar asked Jul 24 '26 12:07

Jumhyn


1 Answers

Did you implement the NSResponder method acceptsFirstResponder to return YES?

- (BOOL)acceptsFirstResponder {
    return YES;
}
like image 76
Ewe Avatar answered Jul 26 '26 01:07

Ewe