Main Content

Pass Mouse Click to Obscured Object

This example shows how to pass mouse clicks to an obscured object.

Set the PickableParts property of a graphics object to none to prevent the object from capturing a mouse click. This example:

  • Defines a context menu for the axes that calls hold with values on or off

  • Creates graphs in which none of the data objects can capture mouse clicks, enabling all right-clicks to pass to the axes and invoke the context menu.

The axesHoldCM function defines a context menu and returns its handle.

function cmHandle = axesHoldCM
   cmHandle = uicontextmenu;
   uimenu(cmHandle,'Label','hold on','Callback',@holdOn);
   uimenu(cmHandle,'Label','hold off','Callback',@holdOff);
end
function holdOn(~,~)
   fig = gcbf;
   ax = fig.CurrentAxes;
   hold(ax,'on')
end
function holdOff(~,~)
   fig = gcbf;
   ax = fig.CurrentAxes;
   hold(ax,'off')
end

Create a bar graph and set the PickableParts property of the Bar objects:

bar(1:20,'PickableParts','none')

Create the context menu for the current axes:

ax = gca;
ax.ContextMenu = axesHoldCM

Right-click over the bars in the graph and display the axes context menu: