Re-enable keypress capture when de-selecting figure

조회 수: 16 (최근 30일)
jg
jg 2017년 1월 13일
답변: Jordan Ross 2017년 1월 23일
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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by