首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可可/目标-C-从其他窗口层发送/接收点击?

可可/目标-C-从其他窗口层发送/接收点击?
EN

Stack Overflow用户
提问于 2012-11-29 21:27:54
回答 1查看 424关注 0票数 1

我正在开发一个类似于GeekTool的应用程序。我不太熟悉GeekTool在内部是如何工作的,但它的外观和行为类似。基本上,我有一个没有边框的窗口,覆盖整个屏幕,这很好。我目前在'kCGDesktopIconWindowLevel‘层上有一个窗口,但是我不能与桌面上的任何东西交互(移动/打开文件等)。当我在这个级别(kCGDesktopIconWindowLevel-1)下面有一个层的窗口时,我可以与桌面交互,但不能与我的窗口交互,我需要能够交互。我是否可以从上面的一层接收点击,或者将它们发送到较低的层?

顺便说一句,如果你对如何做到这一点有一个更好的想法,但避免这个问题,我会非常感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-30 20:36:04

我建议您在this answer中创建一个像这样的事件点击(从applicationDidFinishLaunching:中删除)

代码语言:javascript
复制
CGEventMask emask;
CFMachPortRef myEventTap;
CFRunLoopSourceRef eventTapRLSrc;

// We only want one kind of event at the moment: Left mouse down
emask = CGEventMaskBit(kCGEventLeftMouseDown);

// Create the Tap
myEventTap = CGEventTapCreate (
    kCGSessionEventTap, // Catch all events for current user session
    kCGTailAppendEventTap, // Append to end of EventTap list
    kCGEventTapOptionListenOnly, // We only listen, we don't modify
    emask,
    &myEventTapCallback,
    NULL // We need no extra data in the callback
);

// Create a RunLoop Source for it
eventTapRLSrc = CFMachPortCreateRunLoopSource(
    kCFAllocatorDefault,
    myEventTap,
    0
);

// Add the source to the current RunLoop
CFRunLoopAddSource(
    CFRunLoopGetCurrent(),
    eventTapRLSrc,
    kCFRunLoopDefaultMode
);

将窗口设置为通常忽略鼠标事件-- [myWindow setIgnoresMouseEvents: YES];

然后你的活动点击将寻找它想要“捕捉”的鼠标点击--就像这样:

代码语言:javascript
复制
static CGEventRef myEventTapCallback (
    CGEventTapProxy proxy,
    CGEventType type,
    CGEventRef event,
    void * refcon
) {
    CGPoint mouseLocation;

    // If we would get different kind of events, we can distinguish them
    // by the variable "type", but we know we only get mouse moved events

    mouseLocation = CGEventGetLocation(event);

    // Figure out if the mouse is clicking on something we want to "catch"
    if (/* want this click */)
       [myWindow setIgnoresMouseEvents: NO];

    // Pass on the event, we must not modify it anyway, we are a listener
    return event;
}

鼠标事件完成后,将窗口返回为忽略鼠标事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13635056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档