1

I've tried to read on apple documentation but I can't find anywhere how to capture key event (space or other) into an NSDocument application.

With initialFirstRepsodner if I've understand well it's not possible to do.

Any idea?

thanks to all! Andrea

ATV
  • 4,116
  • 3
  • 23
  • 42
Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98

5 Answers5

1

I've tried to read on apple documentation but I can't find anywhere how to capture key event (space or other) into an NSDocument application.

What do you want to handle key events for? You need to implement keyDown: somewhere, but exactly where depends on what you intend to do.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I have to start and stop video capture with space key and capture a frame with Enter key. I've implemented keyDown on my NSDocument class, but I don't know where define the first responder in nib file. – Andrea Girardi Oct 07 '09 at 06:33
  • You don't. See this comment of mine where I explain the First Responder icon in IB: http://stackoverflow.com/questions/598455/objective-c-cocoa-first-responder-did-i-get-that-right/598663#598663 – Peter Hosey Oct 07 '09 at 07:51
  • As for your task: It sounds like you want a global hot key. You don't implement that through the Cocoa responder chain at all, but with a Carbon Events hot-key. (Despite the name, these are still supported in 64-bit.) There's also a third-party class named PTHotKey that puts a Cocoa-based API on top of that Carbon-based API. – Peter Hosey Oct 07 '09 at 07:54
  • I need to use SPACE and ENTER key so I think with hot-key is not realizable. With captureOutput I take frame from Cam and store it on a CImageBuffer, with ENTER key the CImageBuffer are saved on disk. I need Space key to start and stop movie record. – Andrea Girardi Oct 08 '09 at 15:12
  • “I need to use SPACE and ENTER key so I think with hot-key is not realizable.” They should be. But why not make them customizable? I think almost everyone would customize them, considering they otherwise wouldn't be able to use space and enter outside of your app. (And who wants to have to switch back to your app to end the recording?) – Peter Hosey Oct 08 '09 at 20:22
  • My application capture video from a echocardiography machine and user usually doesn't need to switch to other application because they are interacting with machine, and yes, you have reason. I try with hot-key! – Andrea Girardi Oct 09 '09 at 16:56
0

If you want to capture all the events going to a window, you can subclass it and override -sendEvent:. If you want to capture all the events in the entire app, you can override the same method in an NSApplication subclass.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • It's not clear for me. I'me newbie in Cocoa dev. I'm working with QTRecorder sample of Apple, so, if I'd like to handle all events, have I to add another class that is a subclass of NSDocument? – Andrea Girardi Oct 06 '09 at 17:27
0

For first, I'd like to thank Peter for help!

I've used hotkey and this sample has been very usefull!

http://dbachrach.com/blog/2005/11/program-global-hotkeys-in-cocoa-easily/

thanks to all! Andrea

Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98
0

First of all you have to create a subclass of NSWindow. In xcode do: File -> New File -> Objective C Class. give a name like "NSWindowMyEvents". That will create 2 files: .h & .m, go to the NSWindowMyEvents.h and make the declaration as follows:

@interface NSWindowMyEvents : NSWindow {

}

Save changes and compile (to be sure that IB reads the new header 0 if it is already open).

Open interface builder and load your nib/xib file that contains your Document/Main Window. Ensure that the "window" outlet of the File's owner is set to your main window. Click on your main Window (the one that you want to get events) and set its class (via Identity inspector cmd+6) to be: NSWindowMyEvents instead of NSWindow that it is now.

Save changes!

Go back to xcode and NSWindowMyEvents.m and paste the following code:

- (void)keyDown:(NSEvent *)theEvent
{
    NSLog(@"keyDown!");
    if ([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask)
    {
        NSLog(@"CommandKey Down!");
    }

    [super keyDown:theEvent];
}

Send the Event to super IF you want, to pass the event to the rest responder chain. You are now handling keyboard Events. Similarly you can handle any event in NSWindowMyEvent.m

Hope that helps....

Vassilis
  • 2,878
  • 1
  • 28
  • 43
  • oh, you can use the following code to handle space ctr ... if([theEvent keyCode] == 36) ... (This is the return key) – Vassilis Nov 04 '10 at 20:06
-1

I would recommend using NSUserDefaults and storing your shared global key combos and then checking keyDown: against those stored preferences and then basing your actions on what key was pressed.

ie: #define kMyKeyCommand @"i"

theprojectabot
  • 1,163
  • 12
  • 19