Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyDown not working

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");
}
like image 306
petermney Avatar asked Sep 03 '25 04:09

petermney


1 Answers

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.

like image 84
Wil Shipley Avatar answered Sep 04 '25 23:09

Wil Shipley