How to set shortcuts for zoom tool in my own GUI?

I wrote a GUI in which the zoom tool was frequently used, so I wanted to give it a shortcut. I wrote the figure's KeyPressFcn as follows, but it didn't work.
Q1. The "Ctrl+0" means zoom out, and it worked just fine. But "Ctrl+E" to zoom on just didn't work. How should I wrote the function `zoomKeys`?
Q2. I wanted to use "ctrl+=", but it was used by the editor to zip the codes. Can I still somehow use "Ctrl+="?
function myfunc1(fig)
fig.KeyPressFcn = @zoomKeys;
hAxes = axes(fig);
% some codes
plot(1:10);
function zoomKeys(~, event)
if strcmp(event.Modifier, 'control')
switch event.Key
case 'E' %'='
zoom;
% zoom(gcf, 'on');
case '0'
zoom out;
end
end
end
end

2 Comments

Using just zoom, will only toggle the mouse zoom on and off.
Pass a value to specifiy how much to zoom in.
zoomfactor = 2;
zoom(zoomfactor);
Well, the question is the shortcut "ctrl+E" just doesn't work, no matter "zoom" or "zoom(2)".

Sign in to comment.

 Accepted Answer

Just tested your code. turns out event key is in lower case. Just to safe (in case of capslock), lets convert the value to upper case before comparison.
function zoomKeys(~, event)
if strcmp(event.Modifier, 'control')
k = upper(event.Key);
% disp(k); % for debugging
switch k
case 'E' %'='
zoom;
% zoom(gcf, 'on');
case '0'
zoom out;
end
end
end

9 Comments

Thanks! The previous code was wrong. And your answer just solved my 1st question.
I wanted to used "ctrl+=" which was more common in lots of softwares. Is that possible?
Q3. when the cursor is in zoom mode, is it possible to use shortcut to exit the zoom mode? So that I can use other shortcuts I defined.
You can uncomment the disp(k). If it is working for "=" you will see it in the console.
For your Q3, you will need to remember if the zoom was enabled before and turn it off. Something like this might work
function zoomKeys(~, event)
if strcmp(event.Modifier, 'control')
persistent zoomenabled;
if isempty(zoomenabled)
zoomenabled = false;
end
k = upper(event.Key);
% disp(k); % for debugging
if k ~= 'E'
zoom off;
zoomenabled = false;
end
switch k
case 'E' %'='
zoom;
zoomenabled = true;
% zoom(gcf, 'on');
case '0'
zoom out;
end
end
end
This zoomenabled seems useless. I changed the function to use it. But still, no key can exit the zoom mode.
function zoomKeys(~, event)
persistent zoomenabled;
if strcmp(event.Modifier, 'control')
if isempty(zoomenabled)
zoomenabled = false;
elseif zoomenabled
zoom off;
end
if ~strcmpi(event.Key, 'e')
zoom off;
zoomenabled = false;
end
switch event.Key
case 'e' %'='
zoom;
zoomenabled = true;
case '0'
zoom out;
end
end
end
By the way, I just found that the shortcut "ctrl+=" was actually "ctrl" + "equal".
It seems as soon as zoom is enabled, the keypress function is replaced
% before
f.KeyPressFcn
ans =
function_handle with value:
@myfunc1/zoomKeys
% after
f.KeyPressFcn
ans =
3×1 cell array
{ @localModeKeyPressFcn}
{1×1 matlab.uitools.internal.uimode }
{@(obj,evd)localKeyPressFcn(obj,evd,hMode)}
On further testing it seems, it works if you just zoom in by a fixed factor.
that is replace zoom with zoom(2) or some other factor.
Thanks! zoom(2) did work.
Just not perfect. zoom(2) would zoom the center of the axes without the cursor being in the zoom mode, so you can't zoom any area.
I guess the zoom mode can't be exit by shortcut in user defined functions.
See if Yair Altman's solution works for you. It is quite an old post, so I don't know if it works for the current version.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!