Using arrow keys in cocoa?

I did a little research on this and I found a question. I implemented the code used there, but nothing happened. Here is the exact code I'm using:

.h file

#import <Cocoa/Cocoa.h> @interface Test : NSView { } -(void)keyUp:(NSEvent*)event; -(void)keyDown:(NSEvent*)event; @end 

.m file

 #import "Test.h" @implementation Test - (void)keyDown:(NSEvent*)event { NSLog(@"A key has been pressed"); switch( [event keyCode] ) { case 126: // up arrow case 125: // down arrow case 124: // right arrow case 123: // left arrow NSLog(@"Arrow key pressed!"); break; default: NSLog(@"Key pressed: %@", event); break; } } @end 

What's wrong? Is there something I need to add to the interface?

EDIT: Something actually happened. I sat at my computer. Nothing more.

+4
source share
1 answer

Implement acceptsFirstResponder :

 - (BOOL)acceptsFirstResponder { return YES; } 

Also, make sure your view is the first responder (for example, by clicking inside the view).

+6
source

All Articles