Thread Subject: How do I give focus to a figure?

Subject: How do I give focus to a figure?

From: Jochen Smolka

Date: 29 Oct, 2007 05:46:02

Message: 1 of 10

Hi,

in my application I have a large number of buttons and an
axis object in a figure. After each button press I would
like to return focus to the figure, so that key presses are
evaluated by the figure's KeyPressFcn. Is there a way to do
this programmatically?

Thanks,

Jochen Smolka

Subject: How do I give focus to a figure?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 29 Oct, 2007 05:50:28

Message: 2 of 10

In article <fg3s2p$jgd$1@fred.mathworks.com>,
Jochen Smolka <ja511@gmx.de> wrote:

>in my application I have a large number of buttons and an
>axis object in a figure. After each button press I would
>like to return focus to the figure, so that key presses are
>evaluated by the figure's KeyPressFcn. Is there a way to do
>this programmatically?

Possibly,

set(0,'CurrentFigure',figurenumber)

This will not raise the figure like figure(figurenumber) would.


I'm not sure from your question: do you have a single figure
which contains the GUI and the axis object both? Or do you have
two figures, one with the GUI and the other one the one that
contains the axis object that you want to give control over to?
--
   So you found your solution
   What will be your last contribution?
   -- Supertramp (Fool's Overture)

Subject: How do I give focus to a figure?

From: Jochen Smolka

Date: 29 Oct, 2007 06:01:41

Message: 3 of 10

Thanks for your fast reply,

both the buttons and the axis are in the same figure.
Therefore, unfortunately, neither
> set(0,'CurrentFigure',figurenumber)
nor
> figure(figurenumber)
work, because afterwards the focus (and 'CurrentObject') are
still on the recently pressed button.

I'd be grateful for any other ideas,

Jochen

Subject: How do I give focus to a figure?

From: Jos

Date: 29 Oct, 2007 07:45:26

Message: 4 of 10

"Jochen Smolka" <ja511@gmx.de> wrote in message
<fg3t05$85b$1@fred.mathworks.com>...
> Thanks for your fast reply,
>
> both the buttons and the axis are in the same figure.
> Therefore, unfortunately, neither
> > set(0,'CurrentFigure',figurenumber)
> nor
> > figure(figurenumber)
> work, because afterwards the focus (and 'CurrentObject') are
> still on the recently pressed button.
>
> I'd be grateful for any other ideas,
>
> Jochen
>


May be this will help you:

figure('keypressfcn','disp(''Key press evaluated in
figure'')') ;
h = uicontrol('style','pushbutton',...
 'string','press me', ...
 'callback','disp(''callback of button'')') ;

% (ugly) work around

set(h,'keypressfcn','eval(get(gcbf,''keypressfcn''))')


hth
Jos

Subject: How do I give focus to a figure?

From: Titus

Date: 29 Oct, 2007 09:03:00

Message: 5 of 10


"Jochen Smolka" <ja511@gmx.de> schrieb im Newsbeitrag
news:fg3s2p$jgd$1@fred.mathworks.com...
> Hi,
>
> in my application I have a large number of buttons and an
> axis object in a figure. After each button press I would
> like to return focus to the figure, so that key presses are
> evaluated by the figure's KeyPressFcn. Is there a way to do
> this programmatically?
>
> Thanks,
>
> Jochen Smolka

Hi Jochen,
unfortunately the answer is no. But there is some sort of
workaround: instead of using the callback for the button, do
the following: set the button enable property to "inactive",
and use the buttondownfcn instead of the callback. This
way the button does not get the control so the figure stays
in control...

Titus


Subject: How do I give focus to a figure?

From: Jochen Smolka

Date: 30 Oct, 2007 04:12:57

Message: 6 of 10

Thanks for your replies.

As a quick feedback:
- We have tried a few similar solutions to the 'ugly' work
around, and the problem is that we are getting into trouble
when trying to determine the cursor position during a key
press in the callback. That seems to depend on the button
position.

- The second solution is quite nice, we are using that at
the moment. The only problem I could find is that by
deactivating the buttons we are also loosing the tooltips.
(We added a function now that shows the tooltip in a help
window on right mouse button press).

Thanks again to all of you,

Jochen

Subject: How do I give focus to a figure?

From: Arnaud Delorme

Date: 3 Oct, 2008 06:57:02

Message: 7 of 10

I tried the solution with "buttonpressfunc" on an inactive button and this was not working. I guess the "buttonpressfunc" has to be caught at the figure level. Then you must determine if the mouse position is on top of the button.

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.

Arno

Subject: How do I give focus to a figure?

From: Paul Devonshire

Date: 25 Jun, 2009 16:31:01

Message: 8 of 10

figure(this.FigureHandle);
set(this.FigureHandle, 'HandleVisibility', 'on');

Subject: How do I give focus to a figure?

From: Ryan

Date: 31 Aug, 2009 23:49:19

Message: 9 of 10

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.

Subject: How do I give focus to a figure?

From: Jan Simon

Date: 1 Sep, 2009 19:25:20

Message: 10 of 10

Dear Jochen Smolka!

> in my application I have a large number of buttons and an
> axis object in a figure. After each button press I would
> like to return focus to the figure, so that key presses are
> evaluated by the figure's KeyPressFcn. Is there a way to do
> this programmatically?

See thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/235825

There I explained, that the following piece of code can be inserted in the callbacks of the buttons:
  set(hObject, 'Enable', 'off');
  drawnow;
  set(hObject, 'Enable', 'on');
[hObject] is the handle of the current button. Then the focus has moved to the figure again. Actually this should be performed by "figure(FigureHandle)" also, but this does not work for unknown reasons.
Unfortunately the above trick does not work for Matlab 2008a.

Good luck, Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
key handling Ryan 31 Aug, 2009 19:54:21
gui Ryan 31 Aug, 2009 19:54:21
keypressfcn Ryan 31 Aug, 2009 19:54:21
keypressfcn Jochen Smolka 29 Oct, 2007 01:50:09
figure Jochen Smolka 29 Oct, 2007 01:50:09
focus Jochen Smolka 29 Oct, 2007 01:50:09
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com