Tracking Mouse in ASOC?

Hello!

I’ve been experimenting with ASOC for a few weeks now, and I’m pretty excited, even though some things seem to be more difficult to accomplish than they were with Studio.

One of the things I haven’t still figured out is how to track mouse movements, namely mouse entered and mouse exited. In Studio this was easy, obviously, but how is this done in ASOC? In Objective-C there’s this NSTrackingArea, but even if that’s what I’m looking for, I have no clue how to use it.

To describe my situation, I have a custom view (regular NSView) in a window, and I need to be able to do some things when mouse enters or exits that view (no code to share, this is my starting point).

Any help would be greatly appreciated :slight_smile:

So let’s assume you have a property called custView linked to your NSView. You need something like this:

		set taClass to current application's class "NSTrackingArea"
		set trackingArea to taClass's alloc()'s initWithRect_options_owner_userInfo_(missing value, 545, me, missing value)
		log trackingArea -- helpful for working out enums
		my custView's addTrackingArea_(trackingArea)

Put it in your applicationWillFinishLaunching_ or wherever.

You can replace the first missing value with an NSRect if you only want to use part of the view, depending on the value of the options.

The 545 value is a bit tricky, but the log will give you some feedback. If you look up NSTrackingArea.h, you’ll see all the alternatives and their values. So here I’m using 0x200 (512) for “tracking occurs in visibleRect of view and rect is ignored”, 0x20 (32) for “owner receives […] when view is in key window”, and 0x01 (1) for “owner receives mouseEntered when mouse enters area, and mouseExited when mouse leaves area”. 512 + 32 + 1 = 545. Change to suit.

Then you just need:

	on mouseEntered_(theEvent)
		log "Entered"
	end mouseEntered_
	on mouseExited_(theEvent)
		log "Exited"
	end mouseExited_

Thank you, that was just what I was after, it works like a charm :slight_smile: