Search code examples
objective-cmacoskeyboard-shortcutsnseventkey-events

How to discard command+shift+Q command in mac OS X objective c code?


I'm trying to implement a screensaver simulator on Mac OS X , I managed to disable the effect of pressing command+Q that was causing the application to exit, so now if it's in the full screen mode, it will not respond to the quit keyboard shortcut.

But, my problem is in handling the shortcut of ( Command+ Shift+Q) that pops up the confirmation dialog of Max OS X that warns exiting all the apps and logging of the system.

Can anyone help me in preventing the effect of command+shift+q shortcut while being in full screen mode ?

thanks


Solution

  • This is the best answer for this question

    First in your applicationDidFinishedLoad function, add this peace of code to create an event tap and add the event tap in the current run loop

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

    then in your class, you can implement the call back function called myCGEventCallback like this

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

    for the original code Check Here

    Thanks