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

조회 수: 4 (최근 30일)
Albert Bing
Albert Bing 2020년 1월 26일
댓글: Albert Bing 2020년 1월 28일
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
Mohammad Sami
Mohammad Sami 2020년 1월 26일
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);
Albert Bing
Albert Bing 2020년 1월 26일
편집: Albert Bing 2020년 1월 26일
Well, the question is the shortcut "ctrl+E" just doesn't work, no matter "zoom" or "zoom(2)".

댓글을 달려면 로그인하십시오.

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 1월 26일
편집: Mohammad Sami 2020년 1월 26일
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
Mohammad Sami
Mohammad Sami 2020년 1월 27일
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.
Albert Bing
Albert Bing 2020년 1월 28일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Industrial Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by