• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?


  • Subject: Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
  • From: Martin Wierschin <email@hidden>
  • Date: Tue, 14 Feb 2012 14:54:29 -0800

> I've just been bitten by this.  Seems it would be easy to detect at runtime and assert, is there any magic environment variable or defaults value that can help me catch such incorrect usage?  I've searched but not found...

I don't know if there's any runtime/debug value, but this is one of those times where Objective-C's dynamic nature lets you add what you need. Just swizzle the implementation for -[NSEvent locationInWindow] for debug/development builds, eg:


#ifdef DEBUG
static NSPoint (*_XXEventLocationInWindowReal)( NSEvent* self, SEL _cmd ) = NULL;

static NSPoint _XXEventLocationInWindowOverride( NSEvent* self, SEL _cmd )
{
	NSAssert( [self isMouseEventType], @"Must not ask non-mouse NSEvent's for locationInWindow" );
	return _XXEventLocationInWindowReal( self, _cmd );
}

+ (void) initialize
{
	if( NULL == _XXEventLocationInWindowReal ) {
		Method meth = class_getInstanceMethod( self, @selector(locationInWindow) );
		NSAssert( NULL != meth, @"NSEvent method should exist" );
		_XXEventLocationInWindowReal = (void*)method_setImplementation(meth, (IMP)_XXEventLocationInWindowOverride);
		NSAssert( NULL != _XXEventLocationInWindowReal, @"could not swizzle NSEvent method" );
	}
}
#endif


I'd recommend hiding some of that ugly swizzling code with nice categories on NSObject.

~Martin


_______________________________________________

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


References: 
 >NSEvent locationInWindow undefined for non-mouse events, how to catch? (From: Sean McBride <email@hidden>)

  • Prev by Date: Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
  • Next by Date: Re: -[NSOutlineView ibIsInDesignMode]: unrecognized selector (???)
  • Previous by thread: Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
  • Next by thread: iCal syncing on multiple computers
  • Index(es):
    • Date
    • Thread