How do I tell which key was pressed in a Cocoa Application (I know each key has an associated number)? In my case, I want to log the key to the console.
This is my code:
- (BOOL)acceptsFirstResponder {
return YES;
}
-(void)keyUp:(NSEvent*)event {
NSLog(@"Key %@", event);
}
Use the NSEvent
methods keyCode
, characters
or charactersIgnoringModifiers
.
- (void)keyUp:(NSEvent *)event {
NSLog(@"Characters: %@", [event characters]);
NSLog(@"KeyCode: %hu", [event keyCode]);
}
NSEvent
has the keyCode
method that returns exactly what you're looking for.
来源:https://stackoverflow.com/questions/12770980/detect-which-key-is-pressed