【问题标题】:How to discard command+shift+Q command in mac OS X objective c code?如何在 mac OS X 目标 c 代码中丢弃 command+shift+Q 命令?
【发布时间】:2013-05-08 15:01:28
【问题描述】:

我正在尝试在 Mac OS X 上实现屏幕保护模拟器,我设法禁用了导致应用程序退出的按 command+Q 的效果,所以现在如果它处于全屏模式,它将不会响应到退出键盘快捷键。

但是,我的问题是处理 (Command+Shift+Q) 的快捷方式,它会弹出 Max OS X 的确认对话框,警告退出所有应用程序并记录系统。

谁能帮我在全屏模式下防止 command+shift+q 快捷键的影响?

谢谢

【问题讨论】:

    标签: objective-c macos keyboard-shortcuts nsevent key-events


    【解决方案1】:

    这是这个问题的最佳答案

    首先在你的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

    谢谢

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      相关资源
      最近更新 更多