Using NSTimer and CGEventPost causes problems
Using NSTimer and CGEventPost causes problems
- Subject: Using NSTimer and CGEventPost causes problems
- From: Björn Bollensdorff <email@hidden>
- Date: Tue, 4 Aug 2009 15:51:22 +0200
Hi all,
I'm trying to implement a remote control for an application. The mouse
data is send to the application through a socket which is checked
regularly by a function triggered by a NSTimer. The data is then
transfered into the corresponding CGEvent. Interestingly this works as
long as I do not perform a mouse down on a NSButton or something
similar. If I hit an active element the NSTimer does not fire anymore.
I simplified the problem as much as possible getting to the following
small application that does not use socket communication anymore.
There are two buttons, a checkbox and a label. The first button
triggers the mouse event and the second is the target. The checkbox
sets the position of the mouseEvent. If it is unchecked the mouse down
and up are performed somewhere in the applications window. If it is
checked they are performed on the target button. To see if the action
bound to the target button was triggered the label changes.
If the first button is activated I generate a CGEvent MouseDown and
start a NSTimer. The NSTimer triggers a function that generates a
CGEvent MouseUp. The behavior of the small application now depends on
where the MouseDown is performed. If there is no element underneath
it, the NSTimer fires and the Mouse Up is performed. If an element is
hit the NSTimer does not fire until I manually hit the mouse button.
I'm using CGEventCreateMouseEvent to create the MouseEvent and
CGEventPost to post the mouse event. I tried CGPostMouseEvent, too,
getting the same result.
Does anybody know how to can resolve that problem?
Thanks a lot
Björn
I uploaded a zip-file with the project to:
http://user.cs.tu-berlin.de/~bbolle/MouseEventTest.zip
I'm using Mac OS X 10.5.7 and XCode 3.1.2
The source code of the small application follows:
@implementation EventTest
-(IBAction) startButton:(id)sender
{
NSScreen* mainScreen = [NSScreen mainScreen];
NSRect screenSize = [mainScreen frame];
if([_checkBox intValue]) {
_position = CGPointMake([_window frame].origin.x + 50,
(screenSize.size.height- [_window frame].origin.y-60));
} else {
_position = CGPointMake([_window frame].origin.x + 15,
(screenSize.size.height- [_window frame].origin.y-15));
}
CGEventRef event = CGEventCreateMouseEvent(NULL,
kCGEventLeftMouseDown, _position, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);
_ticker = [[NSTimer scheduledTimerWithTimeInterval: 1.0/60.0 target:
self selector: @selector(timerHandle) userInfo: nil repeats: YES]
retain];
NSLog(@"Timer started");
}
-(IBAction) testButton:(id)sender
{
[_testString setStringValue:@"YES"];
}
-(void) timerHandle
{
NSLog(@"Timer executed");
NSLog(@"Mouse Up");
CGEventRef event = CGEventCreateMouseEvent(NULL,
kCGEventLeftMouseUp, _position, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);
[_ticker invalidate];
[_ticker release];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden