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

6 ビュー (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 2 月 27 日
編集済み: Jan 2017 年 2 月 27 日
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by