MouseLeave message to be sent when object moves away

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

MouseLeave message to be sent when object moves away

Post by andrewferguson » Sat Oct 04, 2014 7:14 am

Hi,

I have an object that has a mouseLeave handler. This handler is run when the mouse moves off the object. However if I keep the mouse still and move the object with the "move" command, mouseLeave is only sent when the user moves their mouse a tiny bit after the object have been moved from under the mouse pointer.

I really need the mouseLeave message to be sent immediately when the object moves away (by the move command) from under the mouse pointer.

Does anyone know how to do this?

Thanks

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: MouseLeave message to be sent when object moves away

Post by dunbarx » Sat Oct 04, 2014 4:23 pm

Hi,

This is similar to a few recent threads that deal with like issues.

No messages are sent during a move. It is necessary to set up a recursive handler after the "move" command line (pseudo):

Code: Select all

...
move whatever
checkForMouseLeave
...

on checkForMouseLeave
put "Still Inside!!!"
if the mouseLoc is not within the rect of whateverControl then
put "Just Left"
exit to top
send checkForMouseLeave in 1
end checkForMouseLeave
Fun stuff.

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: MouseLeave message to be sent when object moves away

Post by FourthWorld » Sat Oct 04, 2014 4:45 pm

One way to do that would be to poll for the state you're looking for (in your case, whether the mouse is within the object) using a timer, e.g.:

Code: Select all

on mouseUp
   set the label of me to "In"
   set the movespeed to 100
   send "PollForMouseWithin" to me in 10 millisecs
   move me rel 200,200 without waiting
end mouseUp

on PollForMouseWithin
   if not within(me, the mouseLoc) then
      set the label of me to "Out" 
   end if
   if "PollForMouseWithin" is not in the pendingMessages \
         AND the movingControls is not empty then
      send "PollForMouseWithin" to me in 10 millisecs
   end if
end PollForMouseWithin
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: MouseLeave message to be sent when object moves away

Post by [-hh] » Sun Oct 05, 2014 7:22 pm

Yet another solution.

Immediately before the move do
send "mouseLeave" to me in 1 millisecs
This goes 'immediately' into the main loop and is done before the move!

Finally, if needed, immediately after the move do
send "mouseEnter" to me in 1 millisecs

To see what I mean and how it works here a test script. Also drag the button and let mouse go up.

Code: Select all

on mouseUp
  -- set rect of me to (200,200,280,280)
   lock messages # <-- avoids flicker
   send "mouseLeave" to me in 1 millisecs
  --> "move me to (0,0) in 1 second" has after dragging
  --> (with grab) a wrong starting point, correct this:
  move me from the mouseloc to (0,0) in 1 second
  move me to (100,100) in 1 second
  if the mouseloc is within the rect of me
  then send "mouseEnter" to me in 1 millisecs
end mouseUp

on mouseEnter
  if there is no grc "Light0" then createOval
  set backcolor of grc "Light0" to "red"
end mouseEnter

on mouseLeave
  set backcolor of grc "Light0" to "green"
end mouseLeave

on mouseDown
  grab me
end mouseDown
on createOval
  create grc "Light0"
  set style of grc "Light0" to oval
  set opaque of grc "Light0" to true
  set rect of grc "Light0" to (0,0,64,64)
end createOval
shiftLock happens

Post Reply