Search code examples
matlabmatlab-deploymentmatlab-figurematlab-guide

Why Seting the SetMode to orbit disables custom KeyPressFcn event handlers, the callback


1-The code below displays the properties of the pressed key.Try it by pressing a key and observe the results.

figure('Name','Press keys to put event data in Command Window',...
         'KeyPressFcn',@(obj,evt)disp(evt));

you will see outputs like this( e.g upon pressing space bar)

    Character: ' '
     Modifier: {1x0 cell}
          Key: 'space'

2-Now simply add the following line of code to above ( or simply execute it before clearing the workspace)

    cameratoolbar('SetMode','orbit');

Now press any key and nothing happens! the control will no longer be transferred to your costume call back function! ( here:@(obj,evt)disp(evt)).

same thing happens for WindowButtonDownFcn, WindowButtonUpFcn too.

How can I get around this? I wanna be able to handle KeyPressFcn or WindowButtonDownFcn after executing cameratoolbar('SetMode','orbit').


Solution

  • I found the answer: Once the cameratoolbar('SetMode','orbit') is called one of these two happens:the handle to the figure is lost or the event handler gets its default value. I am not sure which one though. Therefore we can add the following code to re-assign the lost handler back to our own call back function:

    set(gcf,'KeyPressFcn',@(obj,evt)disp(evt))