views:

183

answers:

2

Hi. 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);
}
A: 

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);
}
nob
Thanks. It's working except for the first time you try to change the active application. Any suggestions?
Rok
maybe starting with an EventNumber of 1?
nob
No, still not working.
Rok
A: 

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

yu2924