Question

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

Was it helpful?

Solution 3

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

OTHER TIPS

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.

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.

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....

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"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top