MATLAB: Mouse button click

buttondownfcnmouse clickpush button

Hello, is it somehow possible to distinguish which mouse button was pressed, left or right?
Thank you

Best Answer

The figure has a property 'SelectionType'. This will have the value 'normal' for a left-click, and 'alt' for a right-click.
For more, see the doc: MATLAB -> User's Guide -> HG Property Browser -> Figure -> SelectionType
Example:
function mybttnfcn(h,~)
hf = get(h,'parent');
b = get(hf,'selectiontype');
xy = get(gca,'CurrentPoint');
if strcmpi(b,'normal')
text(xy(1,1),xy(1,2),'Left click')
elseif strcmpi(b,'alt')
text(xy(1,1),xy(1,2),'Right click')
else
text(xy(1,1),xy(1,2),'Careful there, crazy man!')
end
Then try something like:
figure
plot(rand(5))
set(gca,'buttondownfcn',@mybttnfcn)