views:

174

answers:

5

Hi to all,

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

A: 

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
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
A: 

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
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
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
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
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
“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
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
A: 

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
A: 

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