Re: Mouse move event
Re: Mouse move event
- Subject: Re: Mouse move event
- From: Troy Dawson <email@hidden>
- Date: Wed, 12 Nov 2003 16:51:16 -0800
On Nov 12, 2003, at 3:41 PM, Benjohn wrote:
>
Hi there,
>
>
I'm writing an application a little like the Pixie magnify tool.
>
>
I need to be informed of all mouse move events so that as the user
>
moves their mouse about, the app can redraw its views. I get the
>
impression that mouseMove events are only fired when the mouse is over
>
one of my windows?
>
>
I know I can get the mouse's location from NSEvent, so I could just
>
poll the mouse's position with a periodic, but this doesn't seem like
>
a particularly elegant solution. Can anyone suggest a better way?
I think that's quite an elegant solution -- I've taken out my
NSApplicationMain() and now just drive the event loop like Carbon:
(for this code to work your project's PrincipalClass must be set to
AppWrapper)
@interface AppWrapper : NSApplication
{
SEL _process_func;
App* _app;
}
- (void) process;
@end
@implementation AppWrapper
- (id) init
{
NSLog(@"App init");
self = [super init];
[self setDelegate: self];
NSString* main_nib_name = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"NSMainNibFile"];
bool nib_loaded = main_nib_name && [NSBundle loadNibNamed:
main_nib_name owner: self];
_process_func = nib_loaded ? @selector(eventLoop) :
@selector(initError);
[NSApp finishLaunching];
return self;
}
- (void) dealloc
{
[super dealloc];
}
- (void) initError
{
NSLog(@"Error in application initialization");
}
- (void) eventLoop
{
[NSEvent startPeriodicEventsAfterDelay: 0.0 withPeriod: 0.1];
NSDate* distantFuture = [NSDate distantFuture];
for (;;)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSEvent* event = [NSApp nextEventMatchingMask: NSAnyEventMask
untilDate: distantFuture
inMode: NSDefaultRunLoopMode
dequeue: YES];
NSLog(@"%@", event);
if (event)
[NSApp sendEvent: event];
[pool release];
}
}
- (void) process
{
if (_process_func)
[self performSelector: _process_func];
}
- (void) applicationWillFinishLaunching: (NSNotification*) notification
{
}
- (void) applicationDidFinishLaunching: (NSNotification*) notification
{
NSLog(@"App Interface: init");
_app = [[App create] retain];
}
- (NSApplicationTerminateReply) applicationShouldTerminate:
(NSApplication*) sender
{
return NSTerminateNow;
}
- (void) applicationWillTerminate: (NSNotification*) notification
{
if (_app)
{
[_app release];
_app = nil;
}
}
@end
int main(int argc, const char *argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool allocWithZone: NULL]
init];
NSString* principal_class = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"NSPrincipalClass"];
AppWrapper* app_wrapper = (AppWrapper*)
[NSClassFromString(principal_class) sharedApplication];
if (app_wrapper)
{
if ([app_wrapper respondsToSelector: @selector(process)])
[app_wrapper process];
[app_wrapper release];
}
[pool release];
exit(0);
return 0;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.