Effacer les filtres
Effacer les filtres

WindowButtonDownFcn conflicting with ButtonDownFcn

9 vues (au cours des 30 derniers jours)
Voulgarakis Georgios
Voulgarakis Georgios le 6 Fév 2013
Modifié(e) : Mark le 13 Mai 2014
What I want to achieve is this:
user clicks on an axes: -Run CallbackA
user clicks somewhere else in the figure: -Run CallbackB
What I have done is to set the ButtonDownFcn (CallbackA) of axes, and the WindowButtonDownFcn (CallbackB) of the figure.
Now the thing is that whenever I click on an axes, it runs both Callbacks (both WindowButtonDownFcn and ButtonDownFcn are triggered).
Can I avoid this, and trigger only one of them?

Réponse acceptée

Jan
Jan le 6 Fév 2013
You can insert an axes object spanning this whole figure as a background and use its ButtonDownFcn:
BGcolor = get(0, 'DefaultFigureColor');
BGaxes = axes('Units', 'normalized', 'Position', [0,0,1,1], ...
'XTicks, [], 'YTicks', [], ...
'Color', BGcolor, 'XColor', BGcolor, 'YColor', BGcolor, ...
'ButtonDownFcn', 'disp(''Background'')');
DataAxes = axes('ButtonDownFcn', 'disp(''Data'')';

Plus de réponses (1)

Mark
Mark le 13 Mai 2014
Modifié(e) : Mark le 13 Mai 2014
I was playing around with this recently and noticed that the ButtonDownFcn callback (not WindowButtonDownFcn) for a figure is only activated when the background is clicked. Try this:
hFig = figure;
hAx = axes;
set(hFig, 'ButtonDownFcn', @(~,~) disp('clicked background'));
set(hAx, 'ButtonDownFcn', @(~,~) disp('clicked axes'));
See documentation on figure properties:
"Button press callback function. Executes whenever you press a mouse button while the pointer is in the figure window, but not over a child object"

Catégories

En savoir plus sur Graphics Object Properties dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by