Re-enable keypress capture when de-selecting figure

5 views (last 30 days)
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:
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:
figure_handle = figure(1);
% Fix from
hManager = uigetmodemanager(figure_handle);
[hManager.WindowListenerHandles.Enabled] = deal(false); % HG2
set(figure_handle, 'WindowKeyPressFcn', []);
set(figure_handle, 'KeyPressFcn', @(hobj,evnt) myKeyPress(hobj,evnt));
function myKeyPress(hfig, eventDat)
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.

Answers (1)

Jordan Ross
Jordan Ross on 23 Jan 2017
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.


Find more on Migrate GUIDE Apps 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!

Translated by