program a callback for a keypress

this is the code i wrote:
figure(...,'windowkeypressfcn',@keypress)
function keypress(~,event)
KeyPressed=event.Key;
if strcmp(KeyPressed,'escape')==1
pan off
rotate3d off
zoom out
view(3)
end
end
what i want to do is to set back the graph plotted to it's originam state when i press on escape. but when i run the code after i have plotted the graph and i have navigated through it when i press the escape key the call back don't execute. I this this is because the figure don't have focus, so i will like to know how to give focus to figure.

1 件のコメント

Rik
Rik 2019 年 5 月 13 日
You want to have this keypress function executed without the target figure having the focus?
Also, you should really use explicit handles in your callbacks. In this case it doesn't matter, because the keypress callback can only run when your figure is the current figure, but in other cases this might interfere with other user figures.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2019 年 5 月 13 日

1 投票

Click on the figure to set the focus on it.
According the the documentation this command should set the focus also:
figure(figHandle)
But this did not works since Matlab R4.0. I've tested this some years ago without success, so maybe you still need a workaround:
function FocusToFig(ObjH, EventData)
FigH = ancestor(ObjH, 'figure');
% Work-around
if strcmpi(get(ObjH, 'Type'), 'uicontrol')
set(ObjH, 'Enable', 'off');
drawnow;
set(ObjH, 'Enable', 'on');
pause(0.02); % Give the re-enabled control a chance to be rendered
end
% Methods according to the documentation (does not move the focus for
% keyboard events under Matlab 5.3, 6.5, 2008b, 2009a):
figure(FigH);
set(groot, 'CurrentFigure', FigH);
end
I add this in callbacks of e.g. buttons, such that they give back the focus to the figure. Example:
function ButtonCallback(ButtonH, EventData)
...
FocusToFig(ButtonH);
end

4 件のコメント

Brenda FOCHIVÉ
Brenda FOCHIVÉ 2019 年 5 月 18 日
hello,
thaks for your reply,
I tried your code but it didn't work.
My program plots an array on lines, i used the plot3 function to that.
what i want is that, after the end user has rotated or zoomed on the plot/axes, when he presses escape the axes should go baxk to its original state
Jan
Jan 2019 年 5 月 18 日
編集済み: Jan 2019 年 5 月 18 日
What about using your own tool for the spatial navigation, e.g. https://www.mathworks.com/matlabcentral/fileexchange/39558-figure-rotator ? Then you have full control over the figure and can use the suggested code to return the focus to the figure.
By the way: Which Matlab version are you using? In my tests with R2018b the figure does not loose the focus and the WindowsKeyPressFcn is executed after zooming and rotating in an axes.
Brenda FOCHIVÉ
Brenda FOCHIVÉ 2019 年 5 月 22 日
Hello, I am using MATLAB R2015a
Jan
Jan 2019 年 5 月 23 日
So upgrading is a reliable idea, but it might be expensive. I've written an own tool for the spatial navigation, which uses the same behavior for the mouse keys as in another software, such that the users do not have to learn different styles. It is equivalent to the FEX submission I've posted the link to.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2019 年 5 月 13 日

コメント済み:

Jan
2019 年 5 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by