2013-03-22 134 views
3

根据这个帖子:RegisterEventHotKey CMD + TAB在山狮

ShortcutRecorder record CMD+Tab

呼吁ShortCutRecorder控制setCanCaptureGlobalHotKeys:YES应该让你捕捉CMD + TAB。但是,它似乎并不奏效。如果我使用cmdKey + optionKey,那么它的工作

OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) 
{ 
    NSLog(@"YEAY WE DID A GLOBAL HOTKEY"); 
    return noErr; 
} 

@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    EventHotKeyRef myHotKeyRef; 
    EventHotKeyID myHotKeyID; 
    EventTypeSpec eventType; 

    eventType.eventClass = kEventClassKeyboard; 
    eventType.eventKind = kEventHotKeyPressed; 

    myHotKeyID.signature = 'mhk1'; 
    myHotKeyID.id = 1; 

    InstallApplicationEventHandler(&myHotKeyHandler, 1, &eventType, NULL, NULL); 

    OSStatus status = RegisterEventHotKey(kVK_Tab, 
              cmdKey, 
              myHotKeyID, 
              GetApplicationEventTarget(), 
              0, 
              &myHotKeyRef); 

    NSLog(@"status:%d", status); 
} 

@end 

:我创造了这个小应用自己看看怎么回事。

是否有另一种方式捕捉我自己的应用程序在山狮的CMD + TAB? CMD + OPTION + TAB对我来说不够好。

回答

3

自2010年问这个问题以来,情况有所改变! Dock.app通过Event Tap检测到⌘+,并且该事件不再返回到应用程序。

你仍然可以钩住⌘+,但你需要用一个事件击倒Dock来点击你自己。下面是一些示例代码,礼貌osxbook.com

// alterkeys.c 
// http://osxbook.com 
// 
// Complile using the following command line: 
//  gcc -Wall -o alterkeys alterkeys.c -framework ApplicationServices 
// 
// You need superuser privileges to create the event tap, unless accessibility 
// is enabled. To do so, select the "Enable access for assistive devices" 
// checkbox in the Universal Access system preference pane. 

#include <ApplicationServices/ApplicationServices.h> 

// This callback will be invoked every time there is a keystroke. 
// 
CGEventRef 
myCGEventCallback(CGEventTapProxy proxy, CGEventType type, 
        CGEventRef event, void *refcon) 
{ 
    // Paranoid sanity check. 
    if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp)) 
     return event; 

    // The incoming keycode. 
    CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(
             event, kCGKeyboardEventKeycode); 

    // Swap 'a' (keycode=0) and 'z' (keycode=6). 
    if (keycode == (CGKeyCode)0) 
     keycode = (CGKeyCode)6; 
    else if (keycode == (CGKeyCode)6) 
     keycode = (CGKeyCode)0; 

    // Set the modified keycode field in the event. 
    CGEventSetIntegerValueField(
     event, kCGKeyboardEventKeycode, (int64_t)keycode); 

    // We must return the event for it to be useful. 
    return event; 
} 

int 
main(void) 
{ 
    CFMachPortRef  eventTap; 
    CGEventMask  eventMask; 
    CFRunLoopSourceRef runLoopSource; 

    // Create an event tap. We are interested in key presses. 
    eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp)); 
    eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, 
           eventMask, myCGEventCallback, NULL); 
    if (!eventTap) { 
     fprintf(stderr, "failed to create event tap\n"); 
     exit(1); 
    } 

    // Create a run loop source. 
    runLoopSource = CFMachPortCreateRunLoopSource(
         kCFAllocatorDefault, eventTap, 0); 

    // Add to the current run loop. 
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, 
         kCFRunLoopCommonModes); 

    // Enable the event tap. 
    CGEventTapEnable(eventTap, true); 

    // Set it all running. 
    CFRunLoopRun(); 

    // In a real program, one would have arranged for cleaning up. 

    exit(0); 
} 

这样做的缺点是,你不能沙箱船是在App Store上使用⌘⇥的应用程序。不仅应该明白为什么在这些环境中不允许使用事件触发器(它们使您能够终止 - 甚至是变更事件),但Dock在⌘+上提供的功能非常有用,不能重新映射到一个不同的键盘快捷键,所以即使WitchSwitch避免它默认使用它。