Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How do I give focus to a figure?
Date: Mon, 31 Aug 2009 23:49:19 +0000 (UTC)
Organization: Sandia National Laboratories
Lines: 50
Message-ID: <h7hnhv$sq9$1@fred.mathworks.com>
References: <fg3s2p$jgd$1@fred.mathworks.com> <h208o5$lk8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1251762559 29513 172.30.248.35 (31 Aug 2009 23:49:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 31 Aug 2009 23:49:19 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 793592
Xref: news.mathworks.com comp.soft-sys.matlab:567431


I have solved this problem in the past by putting a call to my main figure keyPressFcn callback in each of the keyPressFcn callbacks of my buttons, etc. on the GUI. For example, here is an example key handler for any combination of key presses in the main GUI figure:

function figure1_KeyPressFcn(hObject, eventdata, handles)

% Initialize the modifier key values
control = 0;
alt     = 0;
shift   = 0;

currChar = get(handles.figure1,'CurrentCharacter');
currKey  = get(handles.figure1,'CurrentKey');
currMod  = get(handles.figure1,'CurrentModifier');

% Determine which modifiers have been pressed
for i=1:length(currMod)
    switch(currMod{i})
        case 'control'
            control = 1;
        case 'alt'
            alt = 1;
        case 'shift'
            shift = 1;
    end
end

% Key combinations:
if control==1 && strcmp(currKey,'z') % Undo last action
    undo(hObject, eventdata, handles)
elseif control==1 && strcmp(currKey,'y') % Redo last action
    redo(hObject, eventdata, handles)
elseif strcmp(currChar,'a')     % Do "a"
    pbA_Callback(hObject, eventdata, handles)
elseif strcmp(currChar,'b')     % Do "a"
    pbB_Callback(hObject, eventdata, handles)
elseif strcmp(currChar,'c')     % Do "a"
    pbC_Callback(hObject, eventdata, handles)
end


Then all you have to do is add the following code to each push button keyPressFcn:

function pbA_KeyPressFcn(hObject, eventdata, handles)
currChar = get(handles.figure1,'CurrentCharacter');
if ~isequal(currChar,char(13))
	figure1_KeyPressFcn(hObject, eventdata, handles)
else
        pbA_Callback(hObject, eventdata, handles)
end

The char(13) check was added to allow the user to press 'enter' to evaluate the selected button callback. Otherwise, the main figure1_KeyPressFcn is evaluated to determine what to do.