program a callback for a keypress
55 次查看(过去 30 天)
显示 更早的评论
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
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
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 个评论
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 Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!