<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449</link>
    <title>MATLAB Central Newsreader - How do I give focus to a figure?</title>
    <description>Feed for thread: How do I give focus to a figure?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 29 Oct 2007 05:46:02 -0400</pubDate>
      <title>How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#398771</link>
      <author>Jochen Smolka</author>
      <description>Hi, &lt;br&gt;
&lt;br&gt;
in my application I have a large number of buttons and an&lt;br&gt;
axis object in a figure. After each button press I would&lt;br&gt;
like to return focus to the figure, so that key presses are&lt;br&gt;
evaluated by the figure's KeyPressFcn. Is there a way to do&lt;br&gt;
this programmatically?&lt;br&gt;
&lt;br&gt;
Thanks, &lt;br&gt;
&lt;br&gt;
Jochen Smolka</description>
    </item>
    <item>
      <pubDate>Mon, 29 Oct 2007 05:50:28 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#398773</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fg3s2p$jgd$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Jochen Smolka &amp;lt;ja511@gmx.de&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;in my application I have a large number of buttons and an&lt;br&gt;
&amp;gt;axis object in a figure. After each button press I would&lt;br&gt;
&amp;gt;like to return focus to the figure, so that key presses are&lt;br&gt;
&amp;gt;evaluated by the figure's KeyPressFcn. Is there a way to do&lt;br&gt;
&amp;gt;this programmatically?&lt;br&gt;
&lt;br&gt;
Possibly,&lt;br&gt;
&lt;br&gt;
set(0,'CurrentFigure',figurenumber)&lt;br&gt;
&lt;br&gt;
This will not raise the figure like  figure(figurenumber)  would.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I'm not sure from your question: do you have a single figure&lt;br&gt;
which contains the GUI and the axis object both? Or do you have&lt;br&gt;
two figures, one with the GUI and the other one the one that&lt;br&gt;
contains the axis object that you want to give control over to?&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;So you found your solution&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;What will be your last contribution?&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Supertramp (Fool's Overture)</description>
    </item>
    <item>
      <pubDate>Mon, 29 Oct 2007 06:01:41 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#398777</link>
      <author>Jochen Smolka</author>
      <description>Thanks for your fast reply, &lt;br&gt;
&lt;br&gt;
both the buttons and the axis are in the same figure.&lt;br&gt;
Therefore, unfortunately,  neither &lt;br&gt;
&amp;gt; set(0,'CurrentFigure',figurenumber)&lt;br&gt;
nor&lt;br&gt;
&amp;gt; figure(figurenumber)&lt;br&gt;
work, because afterwards the focus (and 'CurrentObject') are&lt;br&gt;
still on the recently pressed button.&lt;br&gt;
&lt;br&gt;
I'd be grateful for any other ideas, &lt;br&gt;
&lt;br&gt;
Jochen</description>
    </item>
    <item>
      <pubDate>Mon, 29 Oct 2007 07:45:26 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#398787</link>
      <author>Jos </author>
      <description>&quot;Jochen Smolka&quot; &amp;lt;ja511@gmx.de&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fg3t05$85b$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thanks for your fast reply, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; both the buttons and the axis are in the same figure.&lt;br&gt;
&amp;gt; Therefore, unfortunately,  neither &lt;br&gt;
&amp;gt; &amp;gt; set(0,'CurrentFigure',figurenumber)&lt;br&gt;
&amp;gt; nor&lt;br&gt;
&amp;gt; &amp;gt; figure(figurenumber)&lt;br&gt;
&amp;gt; work, because afterwards the focus (and 'CurrentObject') are&lt;br&gt;
&amp;gt; still on the recently pressed button.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'd be grateful for any other ideas, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Jochen&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
May be this will help you:&lt;br&gt;
&lt;br&gt;
figure('keypressfcn','disp(''Key press evaluated in&lt;br&gt;
figure'')') ;&lt;br&gt;
h = uicontrol('style','pushbutton',...&lt;br&gt;
&amp;nbsp;'string','press me', ...&lt;br&gt;
&amp;nbsp;'callback','disp(''callback of button'')') ;&lt;br&gt;
&lt;br&gt;
% (ugly) work around&lt;br&gt;
&lt;br&gt;
set(h,'keypressfcn','eval(get(gcbf,''keypressfcn''))')&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Mon, 29 Oct 2007 09:03:00 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#398800</link>
      <author>Titus</author>
      <description>&lt;br&gt;
&quot;Jochen Smolka&quot; &amp;lt;ja511@gmx.de&amp;gt; schrieb im Newsbeitrag &lt;br&gt;
news:fg3s2p$jgd$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; in my application I have a large number of buttons and an&lt;br&gt;
&amp;gt; axis object in a figure. After each button press I would&lt;br&gt;
&amp;gt; like to return focus to the figure, so that key presses are&lt;br&gt;
&amp;gt; evaluated by the figure's KeyPressFcn. Is there a way to do&lt;br&gt;
&amp;gt; this programmatically?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Jochen Smolka&lt;br&gt;
&lt;br&gt;
Hi Jochen,&lt;br&gt;
unfortunately the answer is no. But there is some sort of&lt;br&gt;
workaround: instead of using the callback for the button, do&lt;br&gt;
the following: set the button enable property to &quot;inactive&quot;,&lt;br&gt;
and use the buttondownfcn instead of the callback. This&lt;br&gt;
way the button does not get the control so the figure stays&lt;br&gt;
in control...&lt;br&gt;
&lt;br&gt;
Titus </description>
    </item>
    <item>
      <pubDate>Tue, 30 Oct 2007 04:12:57 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#398954</link>
      <author>Jochen Smolka</author>
      <description>Thanks for your replies. &lt;br&gt;
&lt;br&gt;
As a quick feedback: &lt;br&gt;
- We have tried a few similar solutions to the 'ugly' work&lt;br&gt;
around, and the problem is that we are getting into trouble&lt;br&gt;
when trying to determine the cursor position during a key&lt;br&gt;
press in the callback. That seems to depend on the button&lt;br&gt;
position.&lt;br&gt;
&lt;br&gt;
- The second solution is quite nice, we are using that at&lt;br&gt;
the moment. The only problem I could find is that by&lt;br&gt;
deactivating the buttons we are also loosing the tooltips.&lt;br&gt;
(We added a function now that shows the tooltip in a help&lt;br&gt;
window on right mouse button press).&lt;br&gt;
&lt;br&gt;
Thanks again to all of you, &lt;br&gt;
&lt;br&gt;
Jochen</description>
    </item>
    <item>
      <pubDate>Fri, 03 Oct 2008 06:57:02 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#603521</link>
      <author>Arnaud Delorme</author>
      <description>I tried the solution with &quot;buttonpressfunc&quot; on an inactive button and this was not working. I guess the &quot;buttonpressfunc&quot; has to be caught at the figure level. Then you must determine if the mouse position is on top of the button.&lt;br&gt;
&lt;br&gt;
I had a callback whenever the user press a button on the main figure and this was becoming innactive after pressing the button. I thus simply made a copy of the 'keypressfcn' content from the figure to the button and it works like a charm.&lt;br&gt;
&lt;br&gt;
Arno</description>
    </item>
    <item>
      <pubDate>Thu, 25 Jun 2009 16:31:01 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#660465</link>
      <author>Paul Devonshire</author>
      <description>figure(this.FigureHandle);&lt;br&gt;
set(this.FigureHandle, 'HandleVisibility', 'on');</description>
    </item>
    <item>
      <pubDate>Mon, 31 Aug 2009 23:49:19 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#677196</link>
      <author>Ryan </author>
      <description>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:&lt;br&gt;
&lt;br&gt;
function figure1_KeyPressFcn(hObject, eventdata, handles)&lt;br&gt;
&lt;br&gt;
% Initialize the modifier key values&lt;br&gt;
control = 0;&lt;br&gt;
alt     = 0;&lt;br&gt;
shift   = 0;&lt;br&gt;
&lt;br&gt;
currChar = get(handles.figure1,'CurrentCharacter');&lt;br&gt;
currKey  = get(handles.figure1,'CurrentKey');&lt;br&gt;
currMod  = get(handles.figure1,'CurrentModifier');&lt;br&gt;
&lt;br&gt;
% Determine which modifiers have been pressed&lt;br&gt;
for i=1:length(currMod)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;switch(currMod{i})&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 'control'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;control = 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 'alt'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;alt = 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 'shift'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;shift = 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% Key combinations:&lt;br&gt;
if control==1 &amp;&amp; strcmp(currKey,'z') % Undo last action&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;undo(hObject, eventdata, handles)&lt;br&gt;
elseif control==1 &amp;&amp; strcmp(currKey,'y') % Redo last action&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;redo(hObject, eventdata, handles)&lt;br&gt;
elseif strcmp(currChar,'a')     % Do &quot;a&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pbA_Callback(hObject, eventdata, handles)&lt;br&gt;
elseif strcmp(currChar,'b')     % Do &quot;a&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pbB_Callback(hObject, eventdata, handles)&lt;br&gt;
elseif strcmp(currChar,'c')     % Do &quot;a&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pbC_Callback(hObject, eventdata, handles)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Then all you have to do is add the following code to each push button keyPressFcn:&lt;br&gt;
&lt;br&gt;
function pbA_KeyPressFcn(hObject, eventdata, handles)&lt;br&gt;
currChar = get(handles.figure1,'CurrentCharacter');&lt;br&gt;
if ~isequal(currChar,char(13))&lt;br&gt;
	figure1_KeyPressFcn(hObject, eventdata, handles)&lt;br&gt;
else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pbA_Callback(hObject, eventdata, handles)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
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.</description>
    </item>
    <item>
      <pubDate>Tue, 01 Sep 2009 19:25:20 -0400</pubDate>
      <title>Re: How do I give focus to a figure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158449#677436</link>
      <author>Jan Simon</author>
      <description>Dear Jochen Smolka!&lt;br&gt;
&lt;br&gt;
&amp;gt; in my application I have a large number of buttons and an&lt;br&gt;
&amp;gt; axis object in a figure. After each button press I would&lt;br&gt;
&amp;gt; like to return focus to the figure, so that key presses are&lt;br&gt;
&amp;gt; evaluated by the figure's KeyPressFcn. Is there a way to do&lt;br&gt;
&amp;gt; this programmatically?&lt;br&gt;
&lt;br&gt;
See thread: &lt;a href=&quot;http://www.mathworks.com/matlabcentral/newsreader/view_thread/235825&quot;&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/235825&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
There I explained, that the following piece of code can be inserted in the callbacks of the buttons:&lt;br&gt;
&amp;nbsp;&amp;nbsp;set(hObject, 'Enable', 'off');&lt;br&gt;
&amp;nbsp;&amp;nbsp;drawnow;&lt;br&gt;
&amp;nbsp;&amp;nbsp;set(hObject, 'Enable', 'on');&lt;br&gt;
[hObject] is the handle of the current button. Then the focus has moved to the figure again. Actually this should be performed by &quot;figure(FigureHandle)&quot; also, but this does not work for unknown reasons.&lt;br&gt;
Unfortunately the above trick does not work for Matlab 2008a.&lt;br&gt;
&lt;br&gt;
Good luck, Jan</description>
    </item>
  </channel>
</rss>

