2
votes

I'm trying to perform a certain action by pressing, for example the spacebar (anywhere). In my code, I've got the acceptsFirstResponder method and the keyDown method but I'm not getting an NSLog-message

Here the code:

- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)keyDown:(NSEvent *)theEvent {
NSLog(@"test");
}
2
this code is in my appdelegate.m filepetermney
Of course it doesn't work, the code has to be in a subclass of NSResponder.TheAmateurProgrammer
@TheAmateurProgrammer Ok, I just created a new class (subclass of NSResponder) with the two methods, but I don't get any nslog when I press a keypetermney

2 Answers

4
votes

You need to put your -keyDown: method on an NSView subclass, and that NSView subclass has to be put in a window, and that window has to be on-screen, and you have to click on your view before you hit a key. Then the key will go to your view.

Check the diagram “The Path of Key Events” on this page.

0
votes

In addition to implementing keyDown in your NSView, you also need to implement acceptsFirstResponder and have it return YES:

- (BOOL)acceptsFirstResponder
{
    return YES;
}