Effacer les filtres
Effacer les filtres

program a callback for a keypress

78 vues (au cours des 30 derniers jours)
Brenda FOCHIVÉ
Brenda FOCHIVÉ le 13 Mai 2019
Commenté : Jan le 23 Mai 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 commentaire
Rik
Rik le 13 Mai 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.

Connectez-vous pour commenter.

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Visual Exploration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by