This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

waitforbuttonpress

Wait for click or key press

Syntax

w = waitforbuttonpress

Description

example

w = waitforbuttonpress blocks statements from executing until the user has clicked a mouse button or pressed a key in the current figure.

The return argument, w, can have the following values:

  • 0 if it detects a click

  • 1 if it detects a key press

The waitforbuttonpress function does not return a value when any of the following keys are pressed by themselves or together: Ctrl, Shift, Alt, Caps Lock, Num Lock, or Scroll Lock.

Examples

collapse all

Create a figure and call the waitforbuttonpress function. Then, create axes and note that they do not appear.

figure;
w = waitforbuttonpress;
axes;

Click the figure. Now, waitforbuttonpress returns, execution continues, and the axes appear.

Tips

  • To determine the last key pressed, the mouse selection type, or the location of the mouse pointer within the current figure, query the Figure properties CurrentCharacter, SelectionType, and CurrentPoint, respectively.

Algorithms

Some important points to consider when using waitforbuttonpress:

  • If a WindowButtonDownFcn is defined for the figure, it executes before waitforbuttonpress returns a value.

  • The waitforbuttonpress function errors if the user closes the figure by clicking the window close button unless your code calls the waitforbuttonpress function within a try/catch block.

Compatibility Considerations

expand all

Behavior changed in R2014b

Introduced before R2006a