Can I define a figure event callback that triggers when the mouse enters/leaves the figure window?

7 views (last 30 days)
I would like to know, if I can set up a callback function that is triggered when the mouse cursor leaves or enters the figure window.
function MouseEnteredWindowFcn(hObject, evData)
% do something when the mouse enters the window
end
function MouseLeftWindowFcn(hObject, evData)
% do something when the mouse leaves the window
end
I found MouseEnteredCallback on undocumentedmatlab.com. But I would rather use a more official method with less underlying Java components.
  1 Comment
Rik
Rik on 8 Jun 2022
If you want official things only, you will have to track the cursor with the motion functions, or have a timer that test the position of the cursor periodically.

Sign in to comment.

Answers (1)

Poorna
Poorna on 7 Sep 2023
Hi Florian Berzsenyi,
I understand that you want to know if there are callback properties of mouse enter and exit events in the figure object. But as of R2023a there are no such properties.
You could achieve the required functionality by using WindowButtonMotionFcn, CurrentPoint,Position” properties of figure to detect mouse enter and exit events.
You can maintain a Boolean variable which takes the value true if the pointer is within the window in the previous call to the callback function, or false otherwise. Inside the callback function for WindowButtonMotionFcn, check if the CurrentPoint is within the bounds of the window, if this is true and the if the Boolean variable is false, it means the mouse pointer has just entered the window and you can perform your required operations for mouse entering the window. You could do the same for mouse leaving the window.
Hope this helps!

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!