hockeyman
hockeyman

Reputation: 1183

Reacting to key events

I want to do so that if user press "return" key on his keyboard, app has to do something. I try the following:

- (void)keyDown: (NSEvent *) event {
        if ([event keyCode] == 36) {  
            [self doSmthFunction];
        }
}

but when I press "return" I just hear the beep sound.


Full code

KeyEventController.m

#import "KeyEventController.h"
#include "myCustomClass.h"

@implementation KeyEventController
- (void)keyDown: (NSEvent *) event {
        if ([event keyCode] == 36) {
                NSLog(@"log");
        }
}
@end

KeyEventController.h:

#import <Cocoa/Cocoa.h>

@interface KeyEventController : NSResponder 

@end

Upvotes: 1

Views: 125

Answers (1)

rdelmar
rdelmar

Reputation: 104082

You might have to override acceptsFirstResponder, and return YES. Also, you might need to click in your view ( or whatever it is you subclassed) to have it not beep. I've tested this, and sometimes, it seems necessary to do these things, and sometimes not.

Upvotes: 1

Related Questions