MATLAB: Default behaviour of key press callback for matlab figure

axescallbackfigurekeypressMATLAB

When press a key, matlab automatically brings the command window to the front.
But when I assign a customed keypressfcn to the figure, matlab only responds to the key press I defined in the callback function. Even when I press other keys, it does not swtich from figure to command window automatically.
How can I keep the automatic switching?

Best Answer

Matlab can't know which keys your callback care or doesn't care about, so your callback receives all the key presses and indeed the command window is no longer brought to the foreground.
In my opinion, it's not a good idea to rely on this behaviour. I wasn't even aware of it and personnally, I'd be annoyed if a GUI brought back a command window that I'd minimised.
Noneless, you can set the focus back to the command window within your callback by calling commandwindow:
function yourkeypresscallback(source, eventargs)
%some event handling code eg
if eventargs.Key == 'q' && isempty(eventargs.Modifier)
%do something
end
%now bring focus to command window
commandwindow;
end