每当我按下鼠标左键时,我都希望获得打印鼠标指针的位置。但是,当我按住按钮并移动指针时,我也希望位置也会更新。
这是我从MathWorks网站上获取的示例。我进行了一些修改,以便每当按住鼠标左键时打印鼠标位置。但是,在按住按钮的同时移动鼠标时,它不会更新位置。
function MouseButtonEvents
hFig = figure;
myTimer = timer('Name','MyMouseButtonTimer', ...
'Period',1.0, ...
'StartDelay',0.001, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',@timerCallback);
set(hFig,'WindowButtonDownFcn', @mouseDownCallback, ...
'WindowButtonUpFcn', @mouseUpCallback);
function mouseDownCallback(source,event)
fprintf('mouse button is down!\n');
start(myTimer);
end
function mouseUpCallback(source,event)
stop(myTimer);
fprintf('mouse button is up!\n');
end
function timerCallback(source,event)
fprintf(' mouse button is still down!\n');
mousePos=get(hFig,'CurrentPoint');
disp(['You clicked X:',num2str(mousePos(1)),', Y:',num2str(mousePos(2))]);
end
end