can I manually set WindowButtonDownFcn
and selectively overwrite the right or middle-click, while preserving default behavior? Ultimate goal would be to copy figure to clipboard on some click.
set(gcf,'WindowButtonDownFcn',@(src,~) disp(src.SelectionType)); %this seemingly always overwrites default behavior of figure click
I tried this with following error msgs (scroll right)
listener(gcf,'WindowButtonDownFcn',@(src,~) disp(src.SelectionType)) %Event 'WindowButtonDownFcn' is not defined for class 'matlab.ui.Figure'.
listener(get(gcf,'parent'),'WindowButtonDownFcn',@(src,~) disp(src.SelectionType)) %Event 'WindowButtonDownFcn' is not defined for class 'matlab.ui.Root'
handle(gcf).addlistener(handle(gcf),'WindowButtonDownFcn',@(src,~) disp(src.SelectionType)) %Unrecognized method, property, or field 'addlistener' for class 'matlab.ui.Figure'.
and several more permutation using handle
and event.listener
with no success
Tested in Matlab 2019a.
EDIT: here's a template function to use with modifiers based on matlabgui's kind answer
%copies figure to clipboard when [control]+[right-click] anywhere on figure window (and leaving default functionality intact)
figure; plot(randi(100,1,100)) %random figure
addlistener ( gcf, 'WindowMousePress', @(src,~) myFavFunc(src,[]))
function myFavFunc(src,~)
if strcmp(src.SelectionType,'alt') && numel(src.CurrentModifier)==1 && strcmp(src.CurrentModifier,'control')
print -clipboard -dmeta
disp('copied figure to clipboard')
end
end
I dont know why Matlab hide some of the events for figures, you can get a list here:
hFig = figure;
mc = metaclass(hFig);
disp ( {mc.EventList.Name}' ) ;
From that info you can then add a listener to the mouse press event:
hFig = figure;
addlistener ( hFig, 'WindowMousePress', @(src,~)disp('myCallback' ))
That will leave the standard figure callback alone, instead of the disp command get it to run a function where you look at the figure property SelectionType
to determine which mouse button was pressed. You could extend it to use the CurrentModifier
property to determine if Ctrl
, Shift
or Alt
was pressed to further customise it.