How to Execute Something When a Graphics Object is Left-Clicked?

1 次查看(过去 30 天)
I want the red rectangle to turn green when I only “left-click” (not right-click or middle click) on it. What is the easiest solution?
hax = axes('XLim', [0 4], 'YLim', [0 4]);
r = rectangle('Position', [1 1 1 1], 'FaceColor', 'r');

采纳的回答

Jan
Jan 2017-2-27
编辑:Jan 2017-2-27
function test
FigH = figure;
hax = axes('XLim', [0 4], 'YLim', [0 4]);
r = rectangle('Position', [1 1 1 1], 'FaceColor', 'r', ...
'ButtonDownFcn', {@RectClickCB, FigH});
function RectClickCB(RectH, EventData, FigH)
switch get(FigH, 'SelectionType')
case 'normal' % Left click
set(RectH, 'FaceColor', 'g');
case 'alt' % Right click
case 'extend' % Middle click
otherwise
...
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by