Thread Subject:
Toggle "rotate3d" with "r" key while plot has focus

Subject: Toggle "rotate3d" with "r" key while plot has focus

From: Pieter

Date: 10 Aug, 2012 01:04:15

Message: 1 of 5

Hi,

I'm getting more and more annoyed by the fact that there are no shortcuts for the plotting tools like Rotate, Zoom and Pan. It's just inconvenient to click the relevant button in the toolbar to enable such a tool, perform some actions, and then later click the button again to disable it.

Therefore I wondered, would it be possible to assign a shortcut to these tools, in particular the Rotate tool? In the topic linked below I read more or less exactly the thing I would like to do:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/283529

But unfortunately, the provided solution (in the second post) is not entirely clear to me. What I would like to do is to toggle the Rotate tool by pressing "r" while my plot has focus.

The code below enables the Rotate tool when "r" is pressed -- however, after pressing "r" the focus goes to the Command Window (although the Rotate tool _is_ enabled) instead of keeping the focus on the plot... How can I prevent this from happening?

On to the real problem. Now the Rotate tool is enabled, the callbacks like 'WindowKeyPressFcn' are temporarily disabled. In the URL above there should be a solution to fix this... Could someone explain it, or perhaps write some sample code?

Thanks!

% --------------------------------------------------

...

% Add callback to current figure
set(gcf,'WindowKeyPressFcn',@KeyStuff);
% Or should I use the 'KeyPressFcn' here?
...

function KeyStuff(~,Event)
switch Event.Character
    % R for Rotate
    case 'r'
        % Toggle rotate 3D
        disp('Toggling rotate tool...')
        rotate3d;
end

Subject: Toggle "rotate3d" with "r" key while plot has focus

From: Pieter

Date: 10 Aug, 2012 11:45:12

Message: 2 of 5

Actually, it would be better to enable/disable the Rotate tool with the middle mouse button (click/drag/release scroll wheel), like many CAD programs can do.

Right now, by pressing it (and NOT releasing it yet), I can enable Rotate (so with WindowButtonDownFcn). With WindowButtonUpFcn I can disable it again, like this

% ---

...

if strcmp(get(gcf,'SelectionType'), 'extend')
    disp('Enable Rotate.')
    rotate3d
    
    hManager = uigetmodemanager(gcf);
    set(hManager.WindowListenerHandles,'Enable','off')
    
    set(gcf,'WindowButtonUpFcn',@Released )
end

% ---

function Released(~,~)
if strcmp(get(gcf,'SelectionType'), 'extend')
    disp('Disable Rotate.')
    rotate3d
end

% ---

But then, while moving/dragging the mouse (so with the middle button still being pressed), nothing happens. The Rotate tool _is_ enabled, and the icon is also displayed instead of a pointer... But no actual rotation!

Anyone?

Subject: Toggle "rotate3d" with "r" key while plot has focus

From: Bruno Luong

Date: 10 Aug, 2012 12:30:15

Message: 3 of 5

You need to adapt this when the icon toolbar is clicked.

% Toggle with key

close all
figure()
surf(peaks);
% Add callback to current figure
set(gcf,'KeyPressFcn',@KeyStuff);
% Or should I use the 'KeyPressFcn' here?

function KeyStuff(fig,Event,varargin)

c = Event.Character;
switch c
    % R for Rotate
    case 'r'
        % Toggle rotate 3D
        disp('Toggling rotate tool...');
        h = rotate3d(fig);
        if strcmpi(get(h,'Enable'),'on')
            state = 'off';
        else
            state = 'on';
        end
        rotate3d(fig,state);
        RestoreKey(fig, Event);
end
end % KeyStuff

function RestoreKey(fig,Event)
hManager = uigetmodemanager(fig);
set(hManager.WindowListenerHandles,'Enable','off');
CallBacksAfter = get(fig, 'KeyPressFcn');
if isempty(CallBacksAfter)
    CallBacksAfter = {};
elseif ~iscell(CallBacksAfter)
    if isequal(CallBacksAfter,@RestoreKey)
        return
    end
    CallBacksAfter = {'CallBacksAfter'};
end
CallBacksAfter = [{@KeyStuff}; CallBacksAfter];
set(fig, 'KeyPressFcn', CallBacksAfter);
end % RestoreKey

Subject: Toggle "rotate3d" with "r" key while plot has focus

From: Pieter

Date: 10 Aug, 2012 13:16:14

Message: 4 of 5

Hi Bruno,

Thanks for your piece of code, it works like a charm! Do you have any idea whether it would be possible to mimic the rotation behaviour of CAD applications, i.e. "dragging" with the middle mouse button (the scroll wheel)? For more information, see my second post.

Subject: Toggle "rotate3d" with "r" key while plot has focus

From: Pieter

Date: 10 Aug, 2012 23:51:09

Message: 5 of 5

"Pieter" wrote in message <k031iu$pfq$1@newscl01ah.mathworks.com>...
Do you have any idea whether it would be possible to mimic the rotation behaviour of CAD applications, i.e. "dragging" with the middle mouse button (the scroll wheel)? For more information, see my second post.

So actually, enabling the Rotate tool with a MouseDown callback is easy, and disabling it again with a MouseUp callback is not difficult as well. See my second post.

However, as mentioned there, when the mouse button is still in a "Down" state, and the mouse is moved, this would normally cause a rotation (because you're dragging in rotation mode). But it seems that after you get in rotation mode, you need an extra MouseDown event before it actually rotates...?

Therefore I wonder, would it be possible to fake this MouseDown event, in order to end up with the intended behaviour (i.e. push the middle/scroll button down to enable rotation mode, move mouse around while the middle button is still down (= dragging, and thereby causing rotation), and finally releasing the middle mouse button and thereby disabling the rotation mode)?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
scroll wheel Pieter 10 Aug, 2012 07:49:14
shortcut Pieter 9 Aug, 2012 21:09:20
tools Pieter 9 Aug, 2012 21:09:20
plot Pieter 9 Aug, 2012 21:09:20
toggle Pieter 9 Aug, 2012 21:09:20
rotate Pieter 9 Aug, 2012 21:09:20
rssFeed for this Thread

Contact us