program a callback for a keypress

67 views (last 30 days)
Brenda FOCHIVÉ
Brenda FOCHIVÉ on 13 May 2019
Commented: Jan on 23 May 2019
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 Comment
Rik
Rik on 13 May 2019
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.

Sign in to comment.

Answers (1)

Jan
Jan on 13 May 2019
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 Comments
Brenda FOCHIVÉ
Brenda FOCHIVÉ on 22 May 2019
Hello, I am using MATLAB R2015a
Jan
Jan on 23 May 2019
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.

Sign in to comment.

Categories

Find more on Visual Exploration 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!