Simulating Mouse Over/Mouse away via scripting

Intermediate

On a couple of projects I’ve worked on I came across the problem of wanting to use OnMouseEnter to show several objects and interact with those several objects without triggering OnMouseLeave. Quite simply if you use OnMouseEnter / OnMouseLeave in one object and go and interact with another object it will trigger OnMouseLeave.

While working on another project I wanted to find a way to track user idle time (if the user was not interacting with the object or anything at all). This is where the idea was born. What this script does is uses your object’s left, top, right, and bottom coordinates to create a virtual box, a ‘hot spot’ on your desktop where your object is. Then by using a timer we track where the mouse is. If it’s in the hot spot it’ll do something. If it’s outside of the hot spot it’ll do something else.

IMPORTANT: Object must not be a child object. It won’t work otherwise.

Let’s do this!

First we’ll just do one object. I’ll just use the default image. Insert this script

Sub Object_OnScriptEnter
object.settimer 1, 10
End Sub

Sub object_ontimer1
'Check where the cursor is
newx= system.CursorX
newy= system.CursorY

'Check where the object is
l = object.Left
r = object.Right
t = object.Top
b = object.Bottom

'Compare the 2 positions
'If mouse position not over object, hide object
If newx < l Or newx > r Or newy < t Or newy > b Then
Object.visible= False
'If mouse position over object, show object
Else
Object.visible= True
End If

End Sub

Go ahead, try it. Cool, huh?

Now for the neat stuff. We’re going to ‘mouse over’ several objects at the same time. As long as the objects are relatively close together this should work. All we need to do is make the hot spot parameters larger to include the other objects. It also helps if you group your objects.

Here’s my layout (excuse my crappy artwork):




As you can see in the script below I’ve added to the object’s right, and bottom & subtracted from the left and top to include the other objects. The additions/subtractions are relative to the widths, heights, and how far away your other objects are from the main object.



Sub Object_OnScriptEnter
object.settimer 1, 10
End Sub

Sub object_ontimer1
'Check where the cursor is
newx= system.CursorX
newy= system.CursorY

'Check where the object is
‘Add or subtract width or height of other objects to parameters
l = object.Left - 64
r = object.Right + 64
t = object.Top - 64
b = object.Bottom + 64

'Compare the 2 positions
'If mouse position not over objects, hide objects
If newx < l Or newx > r Or newy < t Or newy > b Then
Object.visible= False
Desktopx.Object("object2").visible = False
Desktopx.Object("object3").visible = False
Desktopx.Object("object4").visible = False
Desktopx.Object("object5").visible = False
'If mouse position over objects, show objects
Else
Object.visible= True
Desktopx.Object("object2").visible = True
Desktopx.Object("object3").visible = True
Desktopx.Object("object4").visible = True
Desktopx.Object("object5").visible = True
End If

End Sub


This is also much easier if you group your objects and use the For Each statement:

Sub Object_OnScriptEnter
object.settimer 1, 10
End Sub

Sub object_ontimer1
'Check where the cursor is
newx= system.CursorX
newy= system.CursorY

'Check where the object is
‘Add or subtract width or height of other objects to parameters
l = object.Left - 64
r = object.Right + 64
t = object.Top - 64
b = object.Bottom + 64

'Compare the 2 positions
'If mouse position not over objects, hide objects
If newx < l Or newx > r Or newy < t Or newy > b Then
For each elem in DesktopX.GroupObjects("group1")
elem.visible = false
Next
'If mouse position over objects, show objects
Else
For each elem in DesktopX.GroupObjects("group1")
elem.visible = true
Next
End If

End Sub


You can even have more than one hot spot. Make sure that each hotspot has its own If/Else statement instead of one long if/elseif statement.

I have two objects:



Now insert this script into one of them:

Sub Object_OnScriptEnter
object.settimer 1, 10
End Sub

Sub object_ontimer1
'Check where the cursor is
newx= system.CursorX
newy= system.CursorY

'Check where first object is
l = object.Left - 64
r = object.Right + 64
t = object.Top - 64
b = object.Bottom + 64

‘Check where second object is
l2 = desktopx.Object("object2").left
r2 = desktopx.Object("object2").right
t2 = desktopx.Object("object2").top
b2 = desktopx.Object("object2").bottom

'Compare the positions
'If mouse position over object 1, show
If newx < r And newx > l And newy < b And newy > t Then
Object.visible= True
'If mouse position not over object 1, hide
Else
Object.visible= False
End If

‘If mouse position over object 2, show
If newx < r2 And newx > l2 And newy < b2 And newy > t2 Then
Desktopx.Object("6").visible = True
‘If mouse position not over object 2, hide
Else
Desktopx.Object("6").visible = False
End If

End Sub


Ta da!

That’s it! Thanks for reading.
10,889 views 2 replies
Reply #1 Top
Thanks sViz.

I'm just starting out on DesktopX and keen to learn scripting. Your postings (and RomanDA's) are helping me get up to speed.

At the moment I'm learning 'how to' with WMI and scripting. After a couple of mis-starts the fog is beginning to clear (DesktopX 3.49 beta). I'm happy with my progress so far - have built objects that display the drives, volume name, filesystem and open the drive when clicked on. Simple stuff I grant you but its a start.

I'm curious why there seems to be a low amount of postings here and DesktopX in OS Customization.

Ice

Reply #2 Top
I'm glad you found these tuts useful.   

After a couple of mis-starts the fog is beginning to clear


That's exactly how I'd describe my humble beginnings.....and humble present.  

I'm curious why there seems to be a low amount of postings here and DesktopX in OS Customization.


You and me both. A year ago it was more active.........actually I have a very grim theory that the widget(or interest in making it) is dying. But we DXers can prevent that by sharing our knowledge (in tutorials, on forums) and making interesting widgets! Okay, you probably think I sound like a tin-foil hat conspiracy nut now but you get my point.   I'll keep doing what I can so long as there are folks out there that are eager-to-learn.
+1 Loading…