-2

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);
}
mginn
  • 16,036
  • 4
  • 26
  • 54
Viper OS X
  • 291
  • 1
  • 5
  • 14
  • Did you mean key board key ,? – Arpit B Parekh Oct 07 '12 at 17:50
  • 4
    Where did you put that code? You can't just paste random bits of code into a project and expect something useful. You need to understand a bit about Cocoa's architecture to know how even processing works. – bbum Oct 07 '12 at 18:05
  • The code is not random, it is in a controller that would be highly relevant to the task at hand. keyUp() is indeed the correct method in this case, the OP just wanted to know how to extract information from one of its arguments. – mginn Jun 21 '15 at 00:11
  • This is actually not too localized. I've just come across the same problem and can easily see how it would apply. – mginn Jun 21 '15 at 00:12

2 Answers2

4

Use the NSEvent methods keyCode, characters or charactersIgnoringModifiers.

- (void)keyUp:(NSEvent *)event {
    NSLog(@"Characters: %@", [event characters]);
    NSLog(@"KeyCode: %hu", [event keyCode]);
}
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • sorry but I am a beginner. The NSLog does not show anything. – Viper OS X Oct 07 '12 at 17:28
  • In what class do you use this? Where do you init an instance? – DrummerB Oct 07 '12 at 17:29
  • anything I added to my project is the code up there edit with your code – Viper OS X Oct 07 '12 at 17:32
  • 1
    But where did you add that? You might want to start reading some tutorials or documentation, like [Cocoa Fundamentals](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html) or [The Objective-C Programming Language](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163). – DrummerB Oct 07 '12 at 17:35
  • can it be that difficult to say which key is pressed? – Viper OS X Oct 07 '12 at 17:44
  • 1
    It's not difficult at all, but you have to implement this method at the right place. In a subclass of [`NSResponder`](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html) to be more precise. – DrummerB Oct 07 '12 at 17:46
  • 6
    No, it isn't that difficult. If you had never seen a car engine before and someone said "here, change the spark plugs", you'd have no idea what to do even though it is extremely easy. – bbum Oct 07 '12 at 18:04
  • Why don't you guys show a complete code fragment, from the import to the main method, that does this? Really simple question, please if someone can answer with something better than just: "here is the documentation, go read it." – andy Jul 22 '17 at 04:03
0

NSEvent has the keyCode method that returns exactly what you're looking for.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • like this? : `- (void)keyUp:(NSEvent *)event keyCode:(unsigned short)` – Viper OS X Oct 07 '12 at 17:37
  • @Viper, I think you need to go read some documentation or tutorials. I even put a link to the docs in my answer. It seems like you have some pretty fundamental questions about how Objective-C works - you might be better served trying to figure out some of the basics first. – Carl Norum Oct 07 '12 at 17:46