C graphics
C graphics

Reputation: 7458

How to capture mouse movement over a button

Is it possible to capture mouse-over events when a mouse moves over a button and also when the mouse leaves the button (no clicking will be involved). This is easy to do with figures, using the WindowButtonMotionFcn. However, for buttons or other types of objects I dont know.

Upvotes: 1

Views: 1007

Answers (2)

user1401864
user1401864

Reputation:

You can add your own call back function on what you want to do using WindowButtonMotionFcn.

set(gcf,'WindowButtonMotionFcn ',your_callback);

If you google it you might be able to find more information, belows a link that shows some things you can go with figures. If you know the button pos and the cursors pos, you cal always write tests that way.

http://www.mathworks.com/help/techdoc/ref/figure_props.html

Upvotes: 1

slayton
slayton

Reputation: 20319

While I don't think its possible to directly register callbacks with UIControls if you're clever don't actually need to. Its a bit of extra work but if you know the location of the button within the figure you can check to see if the mouse cursor is over the button and have your UI respond accordingly.

One thing to keep in mind is if you create your buttons with Normalized units you'll have to recalculate their position when the figure is resized. You can use the resizeFcn property of a figure to do this. Register a call back that does something like the following:

u = get(button, 'Units');
set(button,'Units');
buttonPosition = get(button,'Position');
set(button, 'Units', u);

While this doesn't provide you with the exact solution you're looking for it should be sufficient to provide you with the functionality you are trying to achieve.

Upvotes: 1

Related Questions