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:
Apply callback to children

Subject: Apply callback to children

From: Erik

Date: 7 Jan, 2009 22:23:01

Message: 1 of 7

Hi there I have a KeyPressFcn call back on a figure of mine and it works fine when only the figure is selected but the figure has buttons and if any of them are selected the KeyPressFcn no longer works is there any way around this?

Thanks

Subject: Apply callback to children

From: Matt Fig

Date: 7 Jan, 2009 22:51:02

Message: 2 of 7

"Erik" <Idont@wantSpam.co> wrote in message <gk3a05$9u7$1@fred.mathworks.com>...
> Hi there I have a KeyPressFcn call back on a figure of mine and it works fine when only the figure is selected but the figure has buttons and if any of them are selected the KeyPressFcn no longer works is there any way around this?
>
> Thanks

Why not set the keypressfcn of the buttons to the same function as the keypressfcn of the figure?

Subject: Apply callback to children

From: Erik

Date: 8 Jan, 2009 18:29:02

Message: 3 of 7

I can apply them to all the buttons but there are many buttons and I was hoping that I wouldnt have to do that.

Subject: Apply callback to children

From: Jiro Doke

Date: 8 Jan, 2009 18:39:02

Message: 4 of 7

"Erik" <Idont@wantSpam.co> wrote in message <gk5gle$l6j$1@fred.mathworks.com>...
> I can apply them to all the buttons but there are many buttons and I was hoping that I wouldnt have to do that.

Erik,

You should set the WindowKeyPressFcn property of the figure. This gets called even if other components in your figure have focus.


jiro

Subject: Apply callback to children

From: Matt Fig

Date: 8 Jan, 2009 20:38:02

Message: 5 of 7

"Jiro Doke" <jiro.doke@mathworks.com> wrote in message
> You should set the WindowKeyPressFcn property of the figure. This gets called even if other components in your figure have focus.
>
>
> jiro

My version of Matlab doesn't have this as a property for figures. I think this may not always work anyway, depending on the components of the GUI and what the keypressfcn does. For example, if you have an edit box that has a callback which does something similar to what the figure's keypressfcn does, but lets you specify a value, will the windowkeypressfcn execute for every value entered into the edit box? Even when you press return?

The way I was thinking about would be simple enough:

ch = get(gcf,'chi'); % Get all the children.
ch = ch(strcmp(get(ch(:),'type'),'uicontrol')) % Take only uicontrols
ch = ch(strcmp(get(ch(:),'style'),'pushbutton')) % Take only pushbuttons
set(ch,'keypressfcn',{@func}) % set their keypressfcn to the same as that of fig.

Note by changing the filtering operations above (lines 2 and 3), one could choose to set the keypressfcn for any children of the figure desired.

Just a thought.

Subject: Apply callback to children

From: Jiro Doke

Date: 8 Jan, 2009 21:28:01

Message: 6 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <gk5o7a$kap$1@fred.mathworks.com>...
> "Jiro Doke" <jiro.doke@mathworks.com> wrote in message
> > You should set the WindowKeyPressFcn property of the figure. This gets called even if other components in your figure have focus.
> >
> >
> > jiro
>
> My version of Matlab doesn't have this as a property for figures. I think this may not always work anyway, depending on the components of the GUI and what the keypressfcn does. For example, if you have an edit box that has a callback which does something similar to what the figure's keypressfcn does, but lets you specify a value, will the windowkeypressfcn execute for every value entered into the edit box? Even when you press return?
>
> The way I was thinking about would be simple enough:
>
> ch = get(gcf,'chi'); % Get all the children.
> ch = ch(strcmp(get(ch(:),'type'),'uicontrol')) % Take only uicontrols
> ch = ch(strcmp(get(ch(:),'style'),'pushbutton')) % Take only pushbuttons
> set(ch,'keypressfcn',{@func}) % set their keypressfcn to the same as that of fig.
>
> Note by changing the filtering operations above (lines 2 and 3), one could choose to set the keypressfcn for any children of the figure desired.
>
> Just a thought.

Yes, WindowKeyPressFcn would get called regardless of which control is in focus. You could do a check in the function to see if the component with focus is an edit box or not, but what you proposed should be fine as well. BTW, you can also use findobj to find the handles:

ch = findobj(figH, 'type', 'uicontrol', 'style', 'pushbutton');


jiro

Subject: Apply callback to children

From: Matt Fig

Date: 8 Jan, 2009 21:39:02

Message: 7 of 7

"Jiro Doke" <jiro.doke@mathworks.com> wrote in message
>BTW, you can also use findobj to find the handles:
>
> ch = findobj(figH, 'type', 'uicontrol', 'style', 'pushbutton');
>
>
> jiro

D-oh!
When am I ever going to learn to use findobj! :)
I guess I am stuck with being a low-level kinda guy when it comes to handle graphics, I never think of findobj. Maybe this will make me! Thanks jiro.

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