Introduction

Matlab Logo 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.

Keypress Histogram



Create the Visual Aspect of the GUI

  1. Start up Matlab, and type guide in the command line.

    command prompt

  2. Choose to create a new GUI using the “Blank GUI(Default)” option.

    blank GUI

  3. Click on axes icon and add an Axes component to the GUI figure.

  4. Your GUI figure should look something like this:

    What your GUI should look like

Pages: 1 2