Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sample script for KeyPressFcn use in GUI

1,459 views
Skip to first unread message

Don Tucker

unread,
Jun 4, 2002, 6:29:35 PM6/4/02
to
I'm trying to have a function called when a user presses the F1 key
(or 'h') when the cursor is over the form. Could someone provide me
with a very simple example script (e.g., a function calls a
disp('Hello'); or something when the user presses the 'h' key on an
empty form)? In addition to the script, what do I place in the
KeyPressFcn entry on the property inspector for the form?

Thanks in advance,
Don

Ramesh Chandra

unread,
Jun 4, 2002, 8:29:54 PM6/4/02
to
Hi Don,


    You could include the following sequence in your keypressfcn ....


if get(handles.figure1,'currentcharacter') == 'h'


   %execute these instructions


end


Note that your keypressfcn executes only when the focus is on your
figure. I donot know how to detect an 'F1 key' press.


hope that helps,
Ramesh

Don Tucker

unread,
Jun 6, 2002, 4:51:33 PM6/6/02
to
Yes, but what do I put in the property inspector slot next to
KeyPressFcn?

I tried putting 'hey' in the slot, and then in the .m file for the
figure defining:

function varargout = hey
if get(handles,'CurrentCharacter') == 'h'
disp('Hey.')
end

But when I actually attempt to execute the key press, I get an error
that 'hey' is an unrecognized function. How do I actually associate a
function call for this event?

Don

"Ramesh Chandra" <itsr...@hotmail.com> wrote in message news:<eeae...@WebX.raydaftYaTP>...

Steven Lord

unread,
Jun 7, 2002, 1:51:15 PM6/7/02
to

"Don Tucker" <rekcu...@yahoo.com> wrote in message
news:52d70c1.02060...@posting.google.com...

> Yes, but what do I put in the property inspector slot next to
> KeyPressFcn?

I would put the string <automatic> in there. When you save the GUI, the
"<automatic>" will be replaced by the syntax necessary to call to a
subfunction of the main GUI and the main GUI m-file will be updated to
include that subfunction. After that, add your KeyPressFcn to that
subfunction.

> I tried putting 'hey' in the slot, and then in the .m file for the
> figure defining:
>
> function varargout = hey
> if get(handles,'CurrentCharacter') == 'h'
> disp('Hey.')
> end
>
> But when I actually attempt to execute the key press, I get an error
> that 'hey' is an unrecognized function.

Function scope will get you every time.

> How do I actually associate a function call for this event?

See above.

--
Steve Lord
sl...@mathworks.com


Ramesh Chandra

unread,
Jun 7, 2002, 7:15:58 PM6/7/02
to
Hello Don,
   
    Right-click on the figure in GUIDE and click "view callbacks".
from there select keypressfcn ... it will be automatically generated
for you in the m file. Include the code in that function.
 
     You can also define your own keypressfcn. The mistake u are
making is that u are not passing the handles structure to your "hey"
function. So using
function varargout = hey (handles) should do the job for you.
0 new messages