How do I continuously read the mouse position as the mouse is moving, without a click event?
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
raym
il 24 Mar 2017
Modificato: MathWorks Support Team
il 24 Giu 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
Categorie
Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!