我正在尝试在Mac OS X上实现一个屏幕保护程序模拟器,我设法禁用了按命令+Q导致应用程序退出的效果,所以现在如果它处于全屏模式,它将不会响应退出键盘快捷键。
但是,我的问题是在处理快捷键(命令+ Shift+Q),弹出Max OS X的确认对话框,警告退出所有应用程序和系统日志。
谁能帮我在全屏模式下防止command+shift+q快捷键的影响?
谢谢
这是这个问题的最佳答案
首先在applicationDidFinishedLoad函数中,添加这段代码来创建事件tap,并在当前运行循环中添加事件tap
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 tapn");
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;
}
为原始代码在这里检查
谢谢