Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to return keyboard focus to main window after callback

Subject: How to return keyboard focus to main window after callback

From: Bob

Date: 10 Sep, 2008 17:15:42

Message: 1 of 28

I am writing a GUI and I have used the menu editor to define some
menus with keyboard shortcuts. After I execute a callback that draws
to axes, updates textbox strings etc, the keyboard shortcuts are
ignored. The shortcuts do become active after I click in the main
window or in the bar at the top.

Any ideas on how I can force the focus to return to the main window so
the user doesn't have to be clicking after executing a callback?

I gave tried putting (where handle_main_figure is the main figure
handle)
figure(handle_main_figure);
or
uicontrol(handle_main_figure);

at the end of my callback function but it does not work.

Bob

Subject: How to return keyboard focus to main window after callback

From: Walter Roberson

Date: 10 Sep, 2008 18:58:28

Message: 2 of 28

Bob wrote:
> I am writing a GUI and I have used the menu editor to define some
> menus with keyboard shortcuts. After I execute a callback that draws
> to axes, updates textbox strings etc, the keyboard shortcuts are
> ignored. The shortcuts do become active after I click in the main
> window or in the bar at the top.

> Any ideas on how I can force the focus to return to the main window so
> the user doesn't have to be clicking after executing a callback?

> I gave tried putting (where handle_main_figure is the main figure
> handle)
> figure(handle_main_figure);

I'm surprised that did not work.

> or
> uicontrol(handle_main_figure);

I wouldn't expect that to work for a figure, only for a uicontrol.
 
> at the end of my callback function but it does not work.

You could -try-

set(0,'CurrentFigure', handle_main_figure);

--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Subject: How to return keyboard focus to main window after callback

From: Bob

Date: 10 Sep, 2008 22:27:04

Message: 3 of 28

On Sep 10, 11:58 am, Walter Roberson <rober...@hushmail.com> wrote:

> You could -try-
>
> set(0,'CurrentFigure', handle_main_figure);
>

Thanks for the response but no luck with that either. I did put this
at the end of my callback:

Msgbox([num2str(gcf) ' ' num2str(handle_main_figure)]);

The popup showed that the two handles are the same! So my main window
is the current figure.

Maybe the current figure does not necessarily have the keyboard
focus??

Any ideas on a function or method to determine which control has the
focus?

Subject: How to return keyboard focus to main window after callback

From: nor ki

Date: 9 Jan, 2009 08:45:03

Message: 4 of 28

Bob <ralvarez@spambob.net> wrote in message <47ac4621-c0c3-4471-9a9e-34f97a0d8c19@n38g2000prl.googlegroups.com>...
> On Sep 10, 11:58 am, Walter Roberson <rober...@hushmail.com> wrote:
>
> > You could -try-
> >
> > set(0,'CurrentFigure', handle_main_figure);
> >
>
> Thanks for the response but no luck with that either. I did put this
> at the end of my callback:
>
> Msgbox([num2str(gcf) ' ' num2str(handle_main_figure)]);
>
> The popup showed that the two handles are the same! So my main window
> is the current figure.
>
> Maybe the current figure does not necessarily have the keyboard
> focus??
>
> Any ideas on a function or method to determine which control has the
> focus?


Hi Bob,

did you find any solution? i am stuck with the same.. and would be grateful for any hint..

cheers
kinor

Subject: How to return keyboard focus to main window after callback

From: Stefan

Date: 3 Feb, 2009 10:39:01

Message: 5 of 28

Did anyone find a method to restore focus to the figure in order that the KeyPressFcn can be called again?

Subject: How to return keyboard focus to main window after callback

From: Stefan

Date: 3 Feb, 2009 10:52:02

Message: 6 of 28

I even tried:
%set focus to uicontrol
uicointrol(ui_handle);
%set the keypress function for uicontrol in focus
set(ui_handle,'KeyPressFcn',@keypress);

But not even that is working!!!

Subject: How to return keyboard focus to main window after callback

From: PR

Date: 8 Feb, 2009 18:10:03

Message: 7 of 28

stuck on the same problem, please help... anyone?

Subject: How to return keyboard focus to main window after callback

From: Am VR

Date: 23 Feb, 2009 17:15:19

Message: 8 of 28

Same problem here...

Subject: How to return keyboard focus to main window after callback

From: Stefan

Date: 30 Apr, 2009 14:20:03

Message: 9 of 28

I still didn't manage to fix this?
Any suggestions???

Subject: How to return keyboard focus to main window after callback

From: Stefan

Date: 30 Apr, 2009 15:42:01

Message: 10 of 28

I found a way where I'm overwriting the all uicontrol keypress functions, but it's not working with keyrelease because most uicontrols don't have keyrelease functions...
Another stupid thing is that when a uicontrol is in focus, the figure DOES NOT update the SelectionType... Now you won't be able to distinguish betwenn a normal mouse click and alt/extend/open...

I'm really tired of all these workarounds, for a problem which should be easy!
Just need to put the focus back to the FIGURE!!!

Subject: How to return keyboard focus to main window after callback

From: Jan Simon

Date: 13 May, 2009 23:51:01

Message: 11 of 28

Dear Bob!

> I am writing a GUI and I have used the menu editor to define some
> menus with keyboard shortcuts. After I execute a callback that draws
> to axes, updates textbox strings etc, the keyboard shortcuts are
> ignored. The shortcuts do become active after I click in the main
> window or in the bar at the top.

I had a similar problem with the callbacks of buttons. After a button is pressed, it keeps the focus and conceals the window's keypressfcn.
For the buttons this is solved by adding this to the end of the callback:
  set(ObjH, 'Enable', 'off');
  drawnow;
  set(ObjH, 'Enable', 'on');
with ObjH is the handle of the calling button. This releases the focus to the figure.

To my surprise, a similar approach with the 'Visible' property fails, because this move the focus to another button, but not back to the figure.

It works in 6.5.1 and 2008b.

Another solution simulates a mouse click:
  robot = java.awt.Robot:
  pos = get(FigureH, 'Position');
  set(0, 'PointerLocation', [pos(1:2)]);
  robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
  robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
Nevertheless, this activates the figure's WindowsButtonDownFcn and fails for docked figures. The old pointer position should be restored and non-pixels units of the figure must be caught.

There must be a simpler method to grant the focus to a certain figure using java!

Good luck, Jan

Subject: How to return keyboard focus to main window after callback

From: Yair Altman

Date: 14 May, 2009 06:34:01

Message: 12 of 28

"Stefan" <s@m.de> wrote in message <gtcgs9$jot$1@fred.mathworks.com>...
> I found a way where I'm overwriting the all uicontrol keypress functions, but it's not working with keyrelease because most uicontrols don't have keyrelease functions...
> Another stupid thing is that when a uicontrol is in focus, the figure DOES NOT update the SelectionType... Now you won't be able to distinguish betwenn a normal mouse click and alt/extend/open...
>
> I'm really tired of all these workarounds, for a problem which should be easy!
> Just need to put the focus back to the FIGURE!!!

it's as simple as calling figure(hFig) and similarly for any Matlab uicontrol: uicontrol(hControl)
This focus-request behavior is documented in the figure and uicontrol functions respectively

Yair Altman
http://UndocumentedMatlab.com
 

Subject: How to return keyboard focus to main window after callback

From: Stefan

Date: 14 May, 2009 11:10:02

Message: 13 of 28

> it's as simple as calling figure(hFig) and similarly for any Matlab uicontrol: uicontrol(hControl)
> This focus-request behavior is documented in the figure and uicontrol functions respectively
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Unfortunally this is not working, at least not in version 7.5.0.xxxx

Subject: How to return keyboard focus to main window after callback

From: Yair Altman

Date: 14 May, 2009 16:32:02

Message: 14 of 28

"Stefan" <s@m.de> wrote in message <gugu6a$50s$1@fred.mathworks.com>...
> > it's as simple as calling figure(hFig) and similarly for any Matlab uicontrol: uicontrol(hControl)
> > This focus-request behavior is documented in the figure and uicontrol functions respectively
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
> >
>
> Unfortunally this is not working, at least not in version 7.5.0.xxxx

An alternate approach uses undocumented java functionality:
warning off MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame
javaFrame = get(hFig,'JavaFrame');
javaFrame.getAxisComponent.requestFocus;

A similar approach could be used to set the focus on any component, once you get its Java handle using the FindJObj utility from the File Exchange (http://www.mathworks.com/matlabcentral/fileexchange/14317 ):
jButton = findjobj(hButton);
jButton.requestFocus;

I saw that you also wanted keyrelease callbacks for uicontrols that don't normally expose them. Look here for a (undocumented) solution that also relies on the underlying Java: http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/ . The bottom line is to again use FindJObj:
jButton = findjobj(hButton);
set(jButton,'KeyReleasedCallback',@myMatlabCallbackFunction);

The same way you can set some ~30 other callbacks: mouse enter/exit, focus gained/lost etc.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: How to return keyboard focus to main window after callback

From: IonIon

Date: 14 Jul, 2009 20:37:35

Message: 15 of 28

Same problem, and none of the suggestions proposed so far works.

In particular I also tried
 set(ObjH, 'Enable', 'off');
 drawnow;
 set(ObjH, 'Enable', 'on'); .... but without success.

Perhaps I should note that in my program the uicontrols are deleted after each click (an image is displayed instead), and are just re-created after the figure disappears. Still, even withy brand new buttons the focus remains mysteriously vanished.

Jan, could you please explain the java workaround in more detail (to sb who has no experience whatsoever with this :-) ). Are there some (java-)scripts which I need to have before this works?

Thanks.

Georg

Subject: How to return keyboard focus to main window after callback

From: Naor Movshovitz

Date: 27 Jul, 2009 22:14:01

Message: 16 of 28

Same problem. At least I'm not alone. Focus should clearly be a separate and visible/setable property for pretty much every object.
Annoying.

Subject: How to return keyboard focus to main window after callback

From: Matt Fig

Date: 27 Jul, 2009 22:38:02

Message: 17 of 28

Could all (or any) of you who are having trouble with this post a very simple, very short example of a GUI which has the problem? Don't forget to provide instructions on how to reproduce the problem. Thanks.

Subject: How to return keyboard focus to main window after callback

From: Naor Movshovitz

Date: 28 Jul, 2009 18:46:06

Message: 18 of 28

I can't attach a file here, but it's the easiest thing to reproduce this:
1)Open GUIIDE and select GUI With UI Controls from the template menu.
2)save as focusexample
3)right click anywhere in the figure (i.e. the grey area, not near any of the controls) and choose from View Callbacks > KeyPressFcn
4)you are taken to a newly created callback. add the line
display(eventdata.Key)
5)save and run. press any key and its name will be printed in the command window. now use the mouse to click on any of the buttons. the button will recieve focus and using the keyboard no longer does anything. there is no way to give focus back to the main figure with the keyboard either. you have to click somewhere in the gui window, away from the controls, and the key pressing display will resume.

So. Can you think of a way to return focus to the figure after clicking a control?
Thanks,
-naor
"Matt Fig" <spamanon@yahoo.com> wrote in message <h4la8a$7eh$1@fred.mathworks.com>...
> Could all (or any) of you who are having trouble with this post a very simple, very short example of a GUI which has the problem? Don't forget to provide instructions on how to reproduce the problem. Thanks.

Subject: How to return keyboard focus to main window after callback

From: Jan Simon

Date: 29 Jul, 2009 02:30:21

Message: 19 of 28

Dear IonIon!

> Jan, could you please explain the java workaround in more detail (to sb who has no experience whatsoever with this :-) ). Are there some (java-)scripts which I need to have before this works?

You do not need any additional java scripts. The robot is part of the awt installed with Matlab. It works in 6.5 and 2008b and most likely for other versions also.
The piece of code simulate a mouse click in the figure background. Therefore it is necessary to move the mouse to a position on that background, which is not easy, if the figure is docked (I cannot get the global positions of a docked window - anyone can?).
If the figure is filled completely with an image, clicking will not help as long as the HitTest property of the image (and underlying axes) is 'on'.

  robot = java.awt.Robot:
  pos = get(FigureH, 'Position');
  set(0, 'PointerLocation', [pos(1:2)]);
  robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
  robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);

I used this to gain focus to a edit UICONTROL in Matlab 6.5 -- UICONTROL(Handle) was introduced in 7.

Nevertheless, FIGURE(FigureHandle) should actually work and it is equivalent to UICONTROL(UIHandle). This is nicer than a settable Focus property, in my opinion.

Jan

Subject: How to return keyboard focus to main window after callback

From: Matt Fig

Date: 29 Jul, 2009 02:50:19

Message: 20 of 28

> Nevertheless, FIGURE(FigureHandle) should actually work and it is equivalent to UICONTROL(UIHandle). This is nicer than a settable Focus property, in my opinion.
>
> Jan

But this doesn't work from within the callback to a uicontrol. I think (at this point at least) that the JAVA approach is the only way to solve this as I understand it. This is because no matter what you do from within the callback, exiting the callback leaves the cbo with the focus. I will keep fiddling with it but that is how I understand it at this point.

Subject: How to return keyboard focus to main window after callback

From: Jan Simon

Date: 29 Jul, 2009 03:17:02

Message: 21 of 28

Dear Naor Movshovitz!

> 1)Open GUIIDE and select GUI With UI Controls from the template menu.
> 2)save as focusexample
> 3)right click anywhere in the figure (i.e. the grey area, not near any of the controls) and choose from View Callbacks > KeyPressFcn
> 4)you are taken to a newly created callback. add the line
> display(eventdata.Key)
> 5)save and run. press any key and its name will be printed in the command window. now use the mouse to click on any of the buttons. the button will recieve focus and using the keyboard no longer does anything. there is no way to give focus back to the main figure with the keyboard either. you have to click somewhere in the gui window, away from the controls, and the key pressing display will resume.
>
> So. Can you think of a way to return focus to the figure after clicking a control?

I repeat myself:
I've inserted this in the callback of the buttons to move the focus back to the figure:
  set(hObject, 'Enable', 'off');
  drawnow;
  set(hObject, 'Enable', 'on');

After pressing button, you can see the dotted box on the button for a short time, than it vanishes again and finally the figure's KeypressFcn works.
This was tested in Matlab6.5 and 2009a.

So the problem of the OP is anywhere else?
Can you find out, which object has the focus after deleting the button?

Good luck, Jan

Subject: How to return keyboard focus to main window after callback

From: Naor Movshovitz

Date: 29 Jul, 2009 06:55:05

Message: 22 of 28

> I repeat myself:
> I've inserted this in the callback of the buttons to move the focus back to the figure:
> set(hObject, 'Enable', 'off');
> drawnow;
> set(hObject, 'Enable', 'on');
> After pressing button, you can see the dotted box on the button for a short time, >than it vanishes again and finally the figure's KeypressFcn works.
> This was tested in Matlab6.5 and 2009a.

Well, in R2008a, windows xp32, this just causes focus to move to the next control. In this example, if you insert these lines to the 'Calculate' button callback, when you press this button the dotted line will appear briefly and then move to the 'Reset' button. If you are very lucky you might find a (one!) control that passes focus back to the figure.
Is this not the behavior in other versions/systems?
Thanks for trying,
-naor

Subject: How to return keyboard focus to main window after callback

From: Jan Simon

Date: 29 Jul, 2009 14:19:01

Message: 23 of 28

Dear Naor Movshovitz!

> > set(hObject, 'Enable', 'off');
> > drawnow;
> > set(hObject, 'Enable', 'on');
> > After pressing button, you can see the dotted box on the button for a short time, >than it vanishes again and finally the figure's KeypressFcn works.
> > This was tested in Matlab6.5 and 2009a.
>
> Well, in R2008a, windows xp32, this just causes focus to move to the next control. In this example, if you insert these lines to the 'Calculate' button callback, when you press this button the dotted line will appear briefly and then move to the 'Reset' button. If you are very lucky you might find a (one!) control that passes focus back to the figure.
> Is this not the behavior in other versions/systems?

The figure get focus after disable/reenable on WinXP32, Matlab 6.5/2008b/2009a.

Jan

Subject: How to return keyboard focus to main window after callback

From: Naor Movshovitz

Date: 29 Jul, 2009 19:15:20

Message: 24 of 28

> The figure get focus after disable/reenable on WinXP32, Matlab 6.5/2008b/2009a.
> Jan
Indeed it does. Just tested it again to be sure:
2008b - behaves just like you said
2008a - behaves just like i said
strange.
-naor

Subject: How to return keyboard focus to main window after callback

From: Cristop

Date: 21 Mar, 2010 07:30:12

Message: 25 of 28

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <h4oeve$32u$1@fred.mathworks.com>...
> Dear Naor Movshovitz!
>
> > 1)Open GUIIDE and select GUI With UI Controls from the template menu.
> > 2)save as focusexample
> > 3)right click anywhere in the figure (i.e. the grey area, not near any of the controls) and choose from View Callbacks > KeyPressFcn
> > 4)you are taken to a newly created callback. add the line
> > display(eventdata.Key)
http://www.wikio.com/article/bad-credit-payday-loans-176415445
> > 5)save and run. press any key and its name will be printed in the command window. now use the mouse to click on any of the buttons. the button will recieve focus and using the keyboard no longer does anything. there is no way to give focus back to the main figure with the keyboard either. you have to click somewhere in the gui window, away from the controls, and the key pressing display will resume.
> >
> > So. Can you think of a way to return focus to the figure after clicking a control?
>
> I repeat myself:
> I've inserted this in the callback of the buttons to move the focus back to the figure:
> set(hObject, 'Enable', 'off');
> drawnow;
> set(hObject, 'Enable', 'on');
>
> After pressing button, you can see the dotted box on the button for a short time, than it vanishes again and finally the figure's KeypressFcn works.
> This was tested in Matlab6.5 and 2009a.
>
> So the problem of the OP is anywhere else?
> Can you find out, which object has the focus after deleting the button?
>
Good luck, Jan

Subject: How to return keyboard focus to main window after callback

From: Heiko

Date: 29 Feb, 2012 07:44:14

Message: 26 of 28

"Naor Movshovitz" wrote in message <h4q748$nps$1@fred.mathworks.com>...
> > The figure get focus after disable/reenable on WinXP32, Matlab 6.5/2008b/2009a.
> > Jan
> Indeed it does. Just tested it again to be sure:
> 2008b - behaves just like you said
> 2008a - behaves just like i said
> strange.
> -naor

A bit late, I know, but maybe a general approach would be to disable/enable all uicontrols. Inspired by this thread I wrote the following function:

    function ReleaseFocus(fig)
        set(findobj(fig, 'Type', 'uicontrol'), 'Enable', 'off');
        drawnow;
        set(findobj(fig, 'Type', 'uicontrol'), 'Enable', 'on');

Of course, this applies only to figures where each uicontrol is enabled.

Heiko

Subject: How to return keyboard focus to main window after callback

From: Andrew

Date: 13 Mar, 2012 18:20:17

Message: 27 of 28

Thank you Heiko. That works splendidly!


"Heiko" wrote in message <jikl0e$5s5$1@newscl01ah.mathworks.com>...
> "Naor Movshovitz" wrote in message <h4q748$nps$1@fred.mathworks.com>...
> > > The figure get focus after disable/reenable on WinXP32, Matlab 6.5/2008b/2009a.
> > > Jan
> > Indeed it does. Just tested it again to be sure:
> > 2008b - behaves just like you said
> > 2008a - behaves just like i said
> > strange.
> > -naor
>
> A bit late, I know, but maybe a general approach would be to disable/enable all uicontrols. Inspired by this thread I wrote the following function:
>
> function ReleaseFocus(fig)
> set(findobj(fig, 'Type', 'uicontrol'), 'Enable', 'off');
> drawnow;
> set(findobj(fig, 'Type', 'uicontrol'), 'Enable', 'on');
>
> Of course, this applies only to figures where each uicontrol is enabled.
>
> Heiko

Subject: How to return keyboard focus to main window after callback

From: tomas

Date: 22 Mar, 2012 16:59:11

Message: 28 of 28

Hello,
I have a similar problem in my GUI. My keyboard shortcuts works properly until I use the fill command for adding a patch object to my plot. While this object exists I can't use keyboard shortcuts, but immediately after deleting the patch object I'm able to use all shortcuts again.
I've tried
    set(hObject, 'enable', 'off');
    drawnow;
    set(hObject, 'enable', 'on');

but it doesn't work for this.

function ReleaseFocus(fig) has no effect as well.

Any ideas how to solve this problem?

Thanks in advance.

Tom

"Andrew " <leifer@fas.harvard.edu.removethis> wrote in message <jjo351$s4v$1@newscl01ah.mathworks.com>...
> Thank you Heiko. That works splendidly!
>
>
> "Heiko" wrote in message <jikl0e$5s5$1@newscl01ah.mathworks.com>...
> > "Naor Movshovitz" wrote in message <h4q748$nps$1@fred.mathworks.com>...
> > > > The figure get focus after disable/reenable on WinXP32, Matlab 6.5/2008b/2009a.
> > > > Jan
> > > Indeed it does. Just tested it again to be sure:
> > > 2008b - behaves just like you said
> > > 2008a - behaves just like i said
> > > strange.
> > > -naor
> >
> > A bit late, I know, but maybe a general approach would be to disable/enable all uicontrols. Inspired by this thread I wrote the following function:
> >
> > function ReleaseFocus(fig)
> > set(findobj(fig, 'Type', 'uicontrol'), 'Enable', 'off');
> > drawnow;
> > set(findobj(fig, 'Type', 'uicontrol'), 'Enable', 'on');
> >
> > Of course, this applies only to figures where each uicontrol is enabled.
> >
> > Heiko

Tags for this Thread

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.

Contact us