Wednesday, December 13, 2017

#1 2009-10-07 03:13:51 pm

tuomas92
Member
From:: Finland
Registered: 2008-02-18
Posts: 14

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 smile

Offline

 

#2 2009-10-07 11:36:48 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Tracking Mouse in ASOC?

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

Applescript:

       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:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2009-10-08 07:33:06 am

tuomas92
Member
From:: Finland
Registered: 2008-02-18
Posts: 14

Re: Tracking Mouse in ASOC?

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)