MATLAB: Does the callback of a selected uicontrol pushbutton not fire when pressing return (ENTER) in MATLAB 7.9 (R2009b)

MATLAB

When I am selecting a pushbutton in a MATLAB GUI, I can trigger its callback by a left click or pressing the space bar. Instead of <SPACE> key press I would rather trigger the button callback by pressing <ENTER> key.

Best Answer

On most standard GUI platforms, pushbuttons do not respond to ENTER key press. The ENTER key is reserved for activating the default control. However you can work around this behavior by using the push buttons KeyPressFcn callback to call the same code like in the following extract from a simple GUI:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%...

myfunc(hObject, handles)
% --- Executes on key press with focus on pushbutton1 and none of its controls.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
%...
if strcmp(eventdata.Key,'return')
disp([eventdata.Key ' key pressed'])
myfunc(hObject, handles)
end
function myfunc(hObject, handles)
disp('myfunc called')