1

Just wondering, how I go about detecting different keystrokes, and then detecting what key has been pressed I tried using this, -(void)keyDown:(NSEvent *)event but didnt seem to get any results. I've also had a search around but didn't find anything. I'm guessing I may have to set up something in interface builder to detect keystrokes? I also think that it has something to do with what is selected, if its a text field something.

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • 2
    possible duplicate of [- (void) keyDown: (NSEvent *) event does not work..](http://stackoverflow.com/questions/1069433/void-keydown-nsevent-event-does-not-work) – Franci Penov Jun 29 '10 at 16:03
  • 2
    Mick waffle: You should be more specific about what you want to detect keystrokes for. There are several ways to handle keystrokes, and which way is best depends on what you're doing. – Peter Hosey Jun 29 '10 at 17:21
  • Hi Peter, sorry, depending on what key is pressed (anything from function keys to characters) I will be just calling my own methods based on what key it is. – Mick waffle Jun 29 '10 at 17:45
  • Mick waffle: That's the definition of handling keystrokes. You still haven't told us what your goal is. – Peter Hosey Jun 29 '10 at 18:52
  • ok, precisely my end goal is to have my program detect when the F1 key is pressed, when it is pressed, it will wait for another Function Key (I am doing this because I need far more shortcuts hen there are function keys) so for Example, when I press F1, then F1 again, I one of my methods to be called. My methods should do different things depending on what shortcut is pressed, It may just be NSLog something to the console, or it might be sending something to a server using HTTP. I already have these functions made, just need them to be called when different function keys are pressed. Thanks. – Mick waffle Jun 29 '10 at 21:21
  • Should these keys work only within your app (e.g., within a document), or globally across the entire system? If the former, should they work when you have no windows open? – Peter Hosey Jun 30 '10 at 19:12
  • they should only work within my app, when it is open and the window is active. – Mick waffle Jun 30 '10 at 22:51
  • Sounds pretty tedious for MacBooks where the fn key must be held down by default to get a standard F key down flag. Most users will never change that setting. – uchuugaka Jan 07 '14 at 09:36

2 Answers2

1

keyDown: method is called only for certain view and it's subviews I think. If you need all keystrokes for your app - check NSEvent class method:

+ (id)addLocalMonitorForEventsMatchingMask:(NSEventMask)mask 
                                   handler:(NSEvent* (^)(NSEvent*))block

Read upon in it Xcode documentation. I presume you're on snow leopard.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
0

I tried using this, - (void)keyDown:(NSEvent *)event but didnt seem to get any results.

What do you mean “using” it?

You need one of your objects to respond to that message. That means you need it to be a responder, and to be in the responder chain whenever it is appropriate for the keystrokes it handles to be pressed.

Depending on what the keypress does, it may be appropriate for a single custom view to handle it; if not, it should probably be the window controller that handles it. Either one should already be in the responder chain at the appropriate times. Whichever way you go, you'll need to subclass either NSView (for a custom view) or NSWindowController.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370