Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
rotate3d removes keyPressFcn?

Subject: rotate3d removes keyPressFcn?

From: Sven

Date: 31 May, 2010 17:23:04

Message: 1 of 4

Hi all,

I am often clicking the various plot exploration tools (rotate, pan, zoom), and I want to make things easier for myself by adding shortcut keys. Consider the following code:

figure('KeyPressFcn','rotate3d')
surf(peaks(30));

My (simplified) intent here is to toggle the rotate3d tool on my figure whenever a key is pressed. This works to turn *on* the rotate3d tool, but once I'm in rotate3d mode, any subsequent key presses get redirected to the MATLAB command line (instead of executing my 'KeyPressFcn' above.

Can anyone point out what is going on here and how I can get my desired functionality?
It seems that you can turn rotate3d *on* with a keypress, but not *off*.

Thanks,
Sven.

Subject: rotate3d removes keyPressFcn?

From: Walter Roberson

Date: 31 May, 2010 17:50:12

Message: 2 of 4

Sven wrote:

> I am often clicking the various plot exploration tools (rotate, pan,
> zoom), and I want to make things easier for myself by adding shortcut
> keys. Consider the following code:
>
> figure('KeyPressFcn','rotate3d')
> surf(peaks(30));
>
> My (simplified) intent here is to toggle the rotate3d tool on my figure
> whenever a key is pressed. This works to turn *on* the rotate3d tool,
> but once I'm in rotate3d mode, any subsequent key presses get redirected
> to the MATLAB command line (instead of executing my 'KeyPressFcn' above.
>
> Can anyone point out what is going on here and how I can get my desired
> functionality?
> It seems that you can turn rotate3d *on* with a keypress, but not *off*.

I'm not certain about rotate3d but I am certain about zoom: it *does*
deactivate your keypressfcn, and it does so in a way that tells Matlab
not to allow you to install your own afterwards.

I get around it this way. This code installs newCB as the new callback;
I have code in my callback to remove itself and restore the old hook.


     oldkeyhook = get(zfigh, 'KeyPressFcn');
     newkeyhook = oldkeyhook;
     newkeyhook{1} = newCB;

     h = zoom(zfigh);
     set(h, 'Enable', 'on');

     hManager = uigetmodemanager(zfigh);
     set(hManager.WindowListenerHandles,'Enable','off'); % zap the listeners
     % with the listners turned off we can put in our own hook
     set(zfigh, 'KeyPressFcn', newkeyhook );

Subject: rotate3d removes keyPressFcn?

From: Sven

Date: 31 May, 2010 18:24:04

Message: 3 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <pFSMn.37741$Ak3.3367@newsfe16.iad>...
> Sven wrote:
>
> > I am often clicking the various plot exploration tools (rotate, pan,
> > zoom), and I want to make things easier for myself by adding shortcut
> > keys. Consider the following code:
> >
> > figure('KeyPressFcn','rotate3d')
> > surf(peaks(30));
> >
> > My (simplified) intent here is to toggle the rotate3d tool on my figure
> > whenever a key is pressed. This works to turn *on* the rotate3d tool,
> > but once I'm in rotate3d mode, any subsequent key presses get redirected
> > to the MATLAB command line (instead of executing my 'KeyPressFcn' above.
> >
> > Can anyone point out what is going on here and how I can get my desired
> > functionality?
> > It seems that you can turn rotate3d *on* with a keypress, but not *off*.
>
> I'm not certain about rotate3d but I am certain about zoom: it *does*
> deactivate your keypressfcn, and it does so in a way that tells Matlab
> not to allow you to install your own afterwards.
>
> I get around it this way. This code installs newCB as the new callback;
> I have code in my callback to remove itself and restore the old hook.
>
>
> oldkeyhook = get(zfigh, 'KeyPressFcn');
> newkeyhook = oldkeyhook;
> newkeyhook{1} = newCB;
>
> h = zoom(zfigh);
> set(h, 'Enable', 'on');
>
> hManager = uigetmodemanager(zfigh);
> set(hManager.WindowListenerHandles,'Enable','off'); % zap the listeners
> % with the listners turned off we can put in our own hook
> set(zfigh, 'KeyPressFcn', newkeyhook );

Thanks, Walter.
I think that it's a reasonably major flaw that doing something as simple as this requires resorting to undocumented MATLAB features. It makes me think that anything approaching a user-friendly 3D interface like those seen in various CAD programs (you know, the Ctrl+left-click for rotate, Ctrl+right-click for zoom, etc) is a long way off. I wish this weren't the case and hope it's high on the agenda for the next release to make it easier to create useful 3D exploration tools.

Cheers,
Sven.

Subject: rotate3d removes keyPressFcn?

From: Yair Altman

Date: 1 Jun, 2010 11:55:13

Message: 4 of 4

Walter Roberson <roberson@hushmail.com> wrote in message
> I'm not certain about rotate3d but I am certain about zoom: it *does*
> deactivate your keypressfcn, and it does so in a way that tells Matlab
> not to allow you to install your own afterwards.

This is correct. In Matlab GUI, a "mode" is a set of defined behaviors that may be set for any figure window and activated/deactivated via the figure's menu, toolbar or programmatically. Examples of predefined modes are plot edit, zoom, pan, rotate3d and data-cursor. Only one mode can be active in a figure at any one time – this is managed via a figure's ModeManager. Activating a figure mode automatically deactivates any other active mode, as can be seen when switching between plot edit, zoom, pan, rotate3d and data-cursor modes.

During an active mode, the ModeManager disables user-configured mouse and keyboard callbacks, in order to prevent interference with the mode's functionality. For example, mouse clicking during zoom mode has a very specific meaning, and user-configured callbacks might interfere with it. Understanding this and taking the proper precautions (for example: chaining the default mode's callback at the beginning or end of our own callback), we can override this default behavior, as Walter has shown above.

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us