program a callback for a keypress

18 ビュー (過去 30 日間)
Brenda FOCHIVÉ
Brenda FOCHIVÉ 2019 年 5 月 13 日
コメント済み: Jan 2019 年 5 月 23 日
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 日
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 月 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.

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by