开发者

keyUp/keyDown problems in Cocoa

开发者 https://www.devze.com 2023-01-29 14:45 出处:网络
I\'m programming an app that will be a basic video game, but the keyUp:(NSEvent ) and keyDown:(NSEvent) methods are failing to execute.

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 NSOpenGLVie开发者_运维技巧w, could that be affecting it?) and other then that I don't see any problem. Any help is greatly appriciated. Thanks :)


Did you implement the NSResponder method acceptsFirstResponder to return YES?

- (BOOL)acceptsFirstResponder {
    return YES;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消