LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Restrict mouse movements

In one of my programs I have a good sized button. I want to restrict the mouse movements to within the boundaries of this button. In other words... I dont want the mouse cursor to leave this button.

 

I've searched and tried different things but cant seem to get it to work.

 

Any ideas?

0 Kudos
Message 1 of 8
(4,358 Views)
Message 2 of 8
(4,343 Views)

Hi,

 

ClipCursor function might be a good one. I didn't know about it before. Thanks for the tip !

 

Actually I made an example using SetCursorPos . I assumed you where using LabVIEW under Windows if this isn't the case, you won't be able to execute this VI.

 

 

 

 

Regards,

Da Helmut
Voir le profil de Maxime M. sur LinkedIn - View Maxime M.'s profile on LinkedIn
Download All
0 Kudos
Message 3 of 8
(4,336 Views)

WayneS1324,

 

Another way to prevent the mouse from 'leaving' a button is to use the Acquire Input Data to get the mouse coordinates and check both the X and Y axis to make sure the mouse has not left the bounds of the button.  If the cursor is outside the button bounds you can use a call to user32.dll to reset the button position.  Attached is a simple example showing how you can do this.  This example can easily be modified to restrict the mouse to any rectangular region.

 

Regards,

 

Sam K

Applications Engineer
National Instruments

www.ni.com/support

 

 

ButtonBoundsBD.jpg

0 Kudos
Message 4 of 8
(4,266 Views)

 


@sam Kristoff wrote:

...  If the cursor is outside the button bounds you can use a call to user32.dll to reset the button position.


 

Hi, Sam,

Just one small remark.

If the cursor outside, then cursor is _already_ outside. So, if the mouse moves quickly, then sometimes you will see it outside of button for short time. In additional you have to poll mouse coordinates. The only way to clip mouse 100% within area and without polling is WinAPI suggested above. Of course, every medal has two sides: disadvantage of this method - if you will break VI's execution, then mouse remain clipped 😉

 

Andrey.

 

0 Kudos
Message 5 of 8
(4,255 Views)

Elegant!

 

Here's an example, thanks for the great tip, Andrey!

 

ClipCursor.png

Message 6 of 8
(4,244 Views)

I am aware of a bug with my above method: you must ensure the Origin of the FP is set to (0,0). This offset can be accounted for programmatically to make the solution more robust, and I'll leave that as an exercise for the reader. Smiley Wink

0 Kudos
Message 7 of 8
(4,236 Views)

 

Elegant!

 

Here's an example, thanks for the great tip, Andrey!

 


 

Nice one ! +1

Da Helmut
Voir le profil de Maxime M. sur LinkedIn - View Maxime M.'s profile on LinkedIn
0 Kudos
Message 8 of 8
(4,228 Views)