Wednesday, June 28, 2017

[Matlab GUI] Create a shortcut key like Ctrl+P on the GUI windows created by GUIDE

Shortcut key can be set in the GUI created by GUIDE.

First, create the 'KeyPressFcn' callback function
Second, Using 'CurrentModifier' and 'eventdata.key', we can implement a shorcut key on the Matlab GUI. 'CurrentModifier' is used to detect if the buttons like Shift and Ctrl are pressed, and 'eventdata.Key' know which button is pressed.

-------------------------------------------------------

function mainGui_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to mainGui (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

modifier = get(handles.mainGui,'CurrentModifier');
% can get the same string with get(handles.mainGui,'CurrentKey');
keyin = eventdata.Key;

if ~isempty(modifier)
    keypressed = sprintf('%s%s%s',modifier{1,1},'+',keyin);
else
    keypressed = keyin;
end

switch keypressed
    case 'f5'
        Run_Callback(hObject, eventdata, handles);
end      
----------------------------------------------------

No comments:

Post a Comment