Re-enable keypress capture when de-selecting figure

9 次查看(过去 30 天)
I have KeyPressFcn for my figure. When you click some other object/command to lose focus of the figure, it is no longer listening for key presses. I've read about this issue in a few places, including here: http://undocumentedmatlab.com/blog/enabling-user-callbacks-during-zoom-pan
I want to be able to do the following after plotting a figure: 1) Click the zoom button and zoom in on an axes 2) Use a keyboard key KeyPressFcn while the zoom button in the toolbar is still depressed.
Unfortunately, I can't get the fix to work. The only way to get my figure KeyPressFcn working again is to de-select the zoom icon. Here's my code:
main.m:
figure_handle = figure(1);
plot(rand(1,10),rand(1,10));
% Fix from http://undocumentedmatlab.com/blog/enabling-user-callbacks-during-zoom-pan
hManager = uigetmodemanager(figure_handle);
[hManager.WindowListenerHandles.Enabled] = deal(false); % HG2
set(figure_handle, 'WindowKeyPressFcn', []);
set(figure_handle, 'KeyPressFcn', @(hobj,evnt) myKeyPress(hobj,evnt));
myKeyPress.m:
function myKeyPress(hfig, eventDat)
disp('success')
end
Any idea what I'm doing wrong? I want the figure key presses to still work if the user tries to zoom/pan an axes, without having to de-select that operation when they're done.

回答(1 个)

Jordan Ross
Jordan Ross 2017-1-23
Hello,
So I was able to get it working by doing the following:
  1. Create the figure and the plot as you did and store the figure handle.
  2. After the zoom button is pressed, disable the window listener as you had earlier. The only change you need to make is to instead pass your function to the "WindowKeyPressFcn" callback instead of the "KeyPressFcn". So the code should look as follows:
hManager = uigetmodemanager(figure_handle);
[hManager.WindowListenerHandles.Enabled] = deal(false); % HG2
set(figure_handle, 'WindowKeyPressFcn', @(hobj,evnt) myKeyPress(hobj,evnt));
I have tried this out on R2016b and was able to achieve the intended result.

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by