2013-05-08 423 views
1

我試圖在Mac OS X上實現一個屏幕保護程序模擬器,我設法禁用導致應用程序退出的命令+ Q的效果,所以現在如果它處於全屏模式,它將不會響應退出鍵盤快捷鍵。如何在mac OS X目標c代碼中丟棄command + shift + Q命令?

但是,我的問題是在處理(Command + Shift + Q)的快捷方式,彈出Max OS X的確認對話框,警告退出系統的所有應用程序和日誌記錄。

任何人都可以幫助我在全屏模式下防止command + shift + q快捷鍵的影響嗎?

感謝

回答

2

這是這個問題

首先在你的applicationDidFinishedLoad功能,添加以下代碼平安創建活動水龍頭在當前運行循環添加事件水龍頭最好的答案

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); 

然後在你的類,可以實現回調函數調用myCGEventCallback這樣

CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, 
       CGEventRef event, void *refcon) 
    { 
// Paranoid sanity check. 
// type will be key down or key up, you can discard the command + q by setting the  kecode to be -1 like this 

if (((type == kCGEventKeyDown) || (type == kCGEventKeyUp))) 
{ 
    CGEventSetIntegerValueField(
           event, kCGKeyboardEventKeycode, (int64_t)-1); 
    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; 
} 

提供原代碼 Check Here

感謝