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

3 次查看(过去 30 天)
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 个评论
Rik
Rik 2022-6-8
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.

请先登录,再进行评论。

回答(1 个)

Poorna
Poorna 2023-9-7
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!

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by