Synthetic click doesn't switch application's menu bar (Mac OS X)

一世执手 提交于 2019-12-05 04:43:21

问题


I'm developing some sort of air mouse application for iPhone platform. This applications connects to one computer service which generates mouse events on Mac OS X. I'm generating this events with CGEventCreateMouseEvent() and CGEventPost(). But I've encountered one problem. Let's say you are using Safari and then you click on free desktop space. If you do this with regular mouse it will hide Safari's top menu bar and show Finder menu bar. But on these synthetic events it doesn't act like that. Do I have to post some other event or set some additional properties?

Here is my code for mouse up, mouse down:

- (void)mouseUp:(int)button {
    int type = (button == LEFT_BUTTON) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
    int mouseButton = (button == LEFT_BUTTON) ? kCGMouseButtonLeft : kCGMouseButtonRight;
    leftMouseDown = (button == LEFT_BUTTON) ? NO : leftMouseDown;
    rightMouseDown = (button == RIGHT_BUTTON) ? NO : rightMouseDown;
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
    CGEventRef event = CGEventCreateMouseEvent (source, type, CGSCurrentInputPointerPosition(), mouseButton);
    CGEventSetType(event, type);
    CGEventPost(kCGHIDEventTap, event);
    CFRelease(event);
}
- (void)mouseDown:(int)button {
    int type = (button == LEFT_BUTTON) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
    int mouseButton = (button == LEFT_BUTTON) ? kCGMouseButtonLeft : kCGMouseButtonRight;
    leftMouseDown = (button == LEFT_BUTTON) ? YES : leftMouseDown;
    rightMouseDown = (button == RIGHT_BUTTON) ? YES : rightMouseDown;
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
    CGEventRef event = CGEventCreateMouseEvent (source, type, CGSCurrentInputPointerPosition(), mouseButton);
    CGEventSetType(event, type);
    CGEventPost(kCGHIDEventTap, event);
    CFRelease(event);
}

回答1:


You can try CGPostMouseEvent which seems to workaround these issue, but has other drawbacks .(e.g. doesn't highlight the icons on the dock correctly when move the pointer on top of it, at least on my machine.

Update

I think i have it. In order to let Finder cange your Application Menu Bar, you have to deliver also the MouseEventNumber Integerfield, e.g. by

if (type == kCGEventLeftMouseDown) {
   eventNumber++;
   CGEventSetIntegerValueField (event, kCGMouseEventNumber, eventNumber);
}
if (type == kCGEventLeftMouseUp) {
   CGEventSetIntegerValueField (event, kCGMouseEventNumber, eventNumber);
}



回答2:


I try eventNumber=16384 and it works! But I simply don't understand what it means.



来源:https://stackoverflow.com/questions/2602224/synthetic-click-doesnt-switch-applications-menu-bar-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!