How do I continuously read the mouse position as the mouse is moving, without a click event?
150 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 27 Juin 2009
Modifié(e) : MathWorks Support Team
le 11 Avr 2023
I want to read the (x, y) position of the mouse continuously as the mouse moves around the figure, without the need for me to explicitly click at a particular position to use the GINPUT or an equivalent function.
Réponse acceptée
MathWorks Support Team
le 27 Juin 2009
It is possible to record the mouse position continuously in MATLAB without the need for mouse-click events to occur. The current cursor (mouse) position on a figure window can be read by using the 'CurrentPoint' property of the figure. Since you need this mouse position to be read each time the mouse moves, you would assign a callback function to the 'WindowButtonMotionFcn' property of the figure window. This callback function would then read this 'CurrentPoint' property mentioned above.
For example, open a new figure window at the MATLAB prompt. Now, we set the 'WindowButtonMotionFcn' property to point to the callback function which will execute each time the mouse moves on the figure. We will create this callback function in a bit, but for now we will call it 'mouseMove'. The following statement assigns this callback function.
set (gcf, 'WindowButtonMotionFcn', @mouseMove);
Now we create this callback function in the MATLAB editor by typing 'edit mouseMove' at the MATLAB command prompt. An example of the callback function is shown below but in your function, you should include the steps that you want to take when the mouse moves.
function mouseMove (object, eventdata)
C = get (gca, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
Save this function in the path and then move your mouse over the figure. The mouse position will continuously be printed in the title of the axes.
4 commentaires
Walter Roberson
le 2 Fév 2016
ginput() uses the axes ButtonDownFcn callback, not the WindowButtonMotionFcn callback.
Walter Roberson
le 21 Mar 2017
Modifié(e) : MathWorks Support Team
le 11 Avr 2023
"The two points indicate the location of the last mouse click, unless there is a WindowButtonMotionFcn callback defined for the figure. If the WindowButtonMotionFcn callback is defined, then the points indicate the last location of the mouse pointer."
Plus de réponses (1)
raym
le 24 Mar 2017
Modifié(e) : MathWorks Support Team
le 24 Juin 2021
edited Jan 7 '15 at 17:07 answered Jan 7 '15 at 7:38 Are you sure MATLAB can only get the mouse co-ordinates within a GUI? It's actually quite simple to get the position of your mouse anywhere on your screen, independent of a GUI.
Use the following:
get(0, 'PointerLocation') Try this by moving your mouse around and invoking this command each time. You will see that the output keeps changing when the mouse moves. You'll see that this works independent of a GUI.
The output of this function will return a two-element array where the first element is the x or column position and the second element is the y or row position of your mouse. Bear in mind that the reference point is with respect to the bottom left corner of your screen. As such, placing your mouse at the bottom left corner of the screen should yield (1,1) while placing your mouse at the top right corner of the screen yields the resolution of your screen.
Now, if you want to continuously get the position of the mouse, consider placing this call in a while loop while pausing for a small amount of time so you don't overload the CPU. Therefore, do something like this:
while condition loc = get(0, 'CurrentPosition');
%// Do something
%...
%...
pause(0.01); %// Pause for 0.01 ms
end
0 commentaires
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!