Matlab GUI Tutorial - Adding Keyboard Shortcuts / Hotkeys to a GUI
18 Dec 2007 Quan Quach 12 comments 6125 views
Introduction
After fielding several questions on using live keyboard input to perform an action on a GUI, I thought it would be a good ideas to create a tutorial on this topic. In this tutorial, you will learn how to use a keyboard input and combination of keyboard inputs to have the GUI perform a designated action through the use of KeyPressFcn. This is a good way to program “hot keys” or “shortcut keys” into your GUI.
As always, we will demonstrate how to do this through a simple example. In this example, we will be creating a histogram that will reflect the number of times the keys 1 through 9 are pressed. Each time the user presses any key from 1 to 9, the histogram will update immediately. When the user presses 0, the process is terminated and any subsequent key presses will not be processed. Towards the end of this tutorial I will also explain how to create combination shortcut keys that use control, alt, and shift. The source files are available here for those who are too impatient to read through the entire tutorial.

Create the Visual Aspect of the GUI
-
Start up Matlab, and type
guidein the command line.
-
Choose to create a new GUI using the “Blank GUI(Default)” option.

-
Click on
and add an Axes component to the GUI figure. -
Your GUI figure should look something like this:

Pages: 1 2
12 Responses to “Matlab GUI Tutorial - Adding Keyboard Shortcuts / Hotkeys to a GUI”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Hi, Quan Quach :
Great job you are doing. Very good tutorial this one.
Is there a a time when you will talk about sharing data (using the handles structure, of course) between subfunctions of a GUI that are not in the same file? For example: I have a GUI and on it I call a function in another file. I want my handles structure to be seen whitin this other function.
Also I would ask you if you have in mind a tutorial about how to use the varargout and the handles.output = hObject in a correct way. I’ve always tried to use them but never reached a satisfactory result.
Thanks in advance,
JanKees
JanKees:
You can learn more about sharing data in this tutorial.
I have a bunch of tutorials that I want to write in the near future, and I will certainly try to include one on the topic that you requested. Thanks for commenting and leaving feedback!
hi,
hi,
wish you a happy new year. when you will post the next tutorial man, daily i check thrice. man its been 12 long days. post a simple tutorial atleast since it’s a new year week.
14 days
Happy New Year Subin,
Thank you for showing your support! We apologize for the lack of updates during the holiday season. A lot of back-end work was taking place during this time as we are revamping the design of the site. You won’t be disappointed :-).
But upon your request, a new matlab tutorial will be featured within a day. Thank you for your patience. And again, we welcome all readers to send in tutorial requests through our contact form.
Hi,
Happy new year!
Keep up the good work. The tutorials are short and to the point.
Thanks
Maurice
Hi,
thanks for the very good blog entry!
Hello my friends

Hi, u r doing a great job i must say by helping people regarding matlab. I have started doing matlab and i know nothing about gui implementation. I have been given a project to design piano. I am sure you would have some idea about how to design piano, I need some tutorial on gui implementation of implementing piano in matlab,means whenever i press any key,it gives sound of the frequencies and also gives gui. I require only the gui implementation. Kindly you can mail me a tutorial on this with the code also if possible. Kindly mail me on leo1inmail@gmail.com as soon as possible, I shall b v thankful to u.
Quan Quach
U are doing an interesting job. Thank you
hello..
i have some prolem to design the GUI by using the mathlab..can u help me to solve this problem?