Matlab GUI Tutorial - Mapping a Keyboard Button to Execute a GUI Callback
24 Jan 2008 Quan Quach 7 comments 4972 views
Introduction
This tutorial will teach you how to map a keyboard press to execute a GUI callback. We will start with a basic adder GUI. Originally, the GUI adds the two numbers together when the user clicks on the “add” button. We will modify the GUI so that the two numbers will add when the user presses the “enter” key. For more information on this topic, visit this post on Adding Shortcut keys / Hot keys to a GUI.

Example: Modifying the Adder GUI
-
First, download the Adder GUI source files here.
-
Next, we want to add the following line in the opening function:
%we must define the KeyPressFcn for the edit text boxes or else the %enter key will not register while the edit text box is active set(handles.figure1,'KeyPressFcn',@myFunction); set(handles.input1_editText,'KeyPressFcn',@myFunction); set(handles.input2_editText,'KeyPressFcn',@myFunction);
-
Next, we want to add the following code at the end of the m-file
function myFunction(src,evnt) %keyPressFcn automatically takes in two inputs %src is the object that was active when the keypress occurred %evnt stores the data for the key pressed %brings in the handles structure in to the function handles = guidata(src); k= evnt.Key; %k is the key that is pressed if strcmp(k,'return') %if enter was pressed pause(0.01) %allows time to update %define hObject as the object of the callback that we are going to use %in this case, we are mapping the enter key to the add_pushbutton %therefore, we define hObject as the add pushbutton %this is done mostly as an error precaution hObject = handles.add_pushbutton; %call the add pushbutton callback. %the middle argument is not used for this callback add_pushbutton_Callback(hObject, [], handles); end
-
And that’s it. Run the GUI and verifies that it works.
Download: Source Files
You can download the source files here.
This is the end of the tutorial.
7 Responses to “Matlab GUI Tutorial - Mapping a Keyboard Button to Execute a GUI Callback”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Hi,
Thanks for your share ^^
I have used your method to modify my GUI and it works well, but I have one quesition.
After I push some of bottoms on my GUI, the keyboard function was inactivate. I have to click outside of my GUI figure, then the keyboard function will work again.
My question is the mouse and keyboard can’t work simultaneously?
Besides, set(handles.figure1,’KeyPressFcn’,@myFunction); is add before or within the opening function? and handles.figure1 which means the GUI figure? if my GUI figure is XX.fig, then should I change it?
Sorry for such silly questions ><”
Thanks for your help in advanced!!
Best Regards,
maoyuan ( in Taiwan )
Hi maoyuansu,
Did you define the keypressFcn for all of the components within your GUI? THat’s the first thing I can think of. And I don’t have Matlab on this PC so I cannot test it unfortuantely.
Quan
Hi, I am trying to call some function with pushbutton and I am having problems with providing arguments to the function.
When I type this:
example= uicontrol(’Style’,'pushbutton’, …
‘Callback’, ‘Y=demo(1);’);
It works fine, but when I type this:
test=1;
example= uicontrol(’Style’,'pushbutton’, …
‘Callback’, ‘Y=demo(test);’);
I got an error message that “test” is undefined.
Can you please help me with a tip on how to use variable as an input argument of callback function?
Regards,
Pero
Hi every body
can any one help me to commande objects WITH KEYBORD created with VRML into Matlab.
Thanks for your help in advanced!!
Best Regards
Wery nice work thanks this code
how can we detect only mouse click on a figure of MATLAB.
i want to differentiate between mouse click and Key Press..
Thanks for the code. I would like to ask something. How can we use the “MyFunction” as a seperate .m file instead of a function. Should we change anything?