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
MathWorks Support Team 2009년 6월 27일
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 2016년 2월 2일
ginput() uses the axes ButtonDownFcn callback, not the WindowButtonMotionFcn callback.
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