开发者

Objective-C: Listen to keyboard shortcuts and act on them

开发者 https://www.devze.com 2023-03-08 08:54 出处:网络
I am developing an app for Mac OS X and I need to listen to keyboard shortcut inputs so I can act on them.

I am developing an app for Mac OS X and I need to listen to keyboard shortcut inputs so I can act on them.

For example:

  • Up and down keys move up and 开发者_Python百科down on a table view.
  • + drops an item.
  • + + N creates a new item.

It shouldn't be restricted to events on the focused control.

Any ideas?

Thanks in advance.


I think your best option* would be +[NSEvent addLocalMonitorForEventsMatchingMask:handler:]. This creates an object which will call a block handler whenever your application receives an event of the specified type. The handling takes place right before your NSApplication dispatches the event to a window, and you have the opportunity to modify the event or stop it from proceeding further.

You can thus catch key down events as they get passed to your app and do whatever you like with them before any controls get a chance to see them. I posted this originally in another question, but here's a snippet for doing things with arrow key presses:

NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){

    switch ([theEvent keyCode]) {
        case 123:    // Left arrow
            NSLog(@"Left behind.");
            break;
        case 124:    // Right arrow
            NSLog(@"Right as always!");
            break;
        case 125:    // Down arrow
            NSLog(@"Downward is Heavenward");
            break;
        case 126:    // Up arrow
            NSLog(@"Up, up, and away!");
            break;
        default:
            break;
    }
    // Return the event, a new event, or, to stop 
    // the event from being dispatched, nil
    return theEvent;
};

// Creates an object we do not own, but must keep track of so that 
// it can be "removed" when we're done; therefore, put it in an ivar.
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask 
                                                 handler:monitorHandler];

See the Event-Handling Guide for some details about what you're supposed to do with that monitor object. Specifically, Apple apparently "discourages" removing it inside of dealloc, but doesn't give a reason.


*So long as you can require Snow Leopard, at least.


You may need to implements callbacks in your application. Take a look to CGEventTapCreate to start listening for hotkeys.

CGEventTapCreate(kCGSessionEventTap,
             kCGTailAppendEventTap,
             kCGEventTapOptionDefault,
             kCGEventKeyDown
             myEventTapCallback,
             NULL);

myEventTapCallback should be conform to CGEventTapCallBack and gets called when a key is pressed. Then, inside myEventTapCallback you'll have enough information on keys pressed and you can implement your custom functionality.


Have a look at the Cocoa Event-Handling Guide. There are a few places you can intercept events before they get to the key view. You can intercept all events in the application by overriding -[NSApplication sendEvent:], or you can intercept events at a per-window level by overriding-[NSWindow sendEvent:].

0

精彩评论

暂无评论...
验证码 换一张
取 消