keydown in objective-C
[UPDATED AFTER FIRST AWNSER] I have tried to find a way to use and implement the keyDown option in Objective C. But when I try it, it always fails...
Can anyone give me an example of how this is done. I understand Objective C good and a full explaination is not needed.
I deleted the method in -(void) keyDown
because it wasn't working.
This is my code now:
开发者_如何学JAVA#import <Cocoa/Cocoa.h>
@interface ViewController : NSView {
IBOutlet id pressLabel;
}
@end
#import "ViewController.h"
@implementation ViewController
-(BOOL) acceptsFirstResponder
{
return YES;
}
-(BOOL) becomeFirstResponder
{
return YES;
}
-(BOOL) resignFirstResponder
{
return YES;
}
-(void)keyDown:(NSEvent *)theEvent
{
NSString *theUpArrow = [NSString stringWithFormat:@"%c",NSUpArrowFunctionKey];
if( [[theEvent characters] isEqualToString:theUpArrow]){
[pressLabel setStringValue:@"Pressed"];
} else {
[super keyDown:theEvent];
}
}
@end
keyDown:
is an NSResponder method, typically implemented in views. This class is named Controller, which would suggest it isn't a view, and thus won't receive key down events. You probably want to put it in a view instead.
[self keyDown:theEvent];
This doesn't solve your problem, but I think for the line above, you want to use super
and not self
. If you use self
it will invoke the same method again, and it will keep invoking the same method over and over, eventually crashing your application once there is no more stack space.
精彩评论