How do I continuously read the mouse position as the mouse is moving, without a click event?

150 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
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 个评论
Walter Roberson
Walter Roberson 2017-3-21
编辑:MathWorks Support Team 2023-4-11
"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."

请先登录,再进行评论。

更多回答(1 个)

raym
raym 2017-3-24
编辑:MathWorks Support Team 2021-6-24
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

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by