waitforbuttonpress

Wait for key press or mouse-button click

    Note:   The behavior of the waitforbuttonpress function changed in R2014b. The figure that is current when you call the waitforbuttonpress function is the only area in which users can press a key or click a mouse button to resume program execution.

Syntax

k = waitforbuttonpress

Description

k = waitforbuttonpress blocks the caller's execution stream until the function detects that the user has clicked a mouse button or pressed a key while the figure window is active. The figure that is current when you call the waitforbuttonpress function is the only area in which users can press a key or click a mouse button to resume program execution. The return argument, k, can have these values:

  • 0 if it detects a mouse button click

  • 1 if it detects a key press

If a WindowButtonDownFcn callback is defined for the figure, its callback is executed before waitforbuttonpress returns a value.

Only keys that generate characters cause the function to return. Pressing any of the following keys by itself does nothing: Ctrl, Shift, Alt, Caps_lock, Num_lock, Scroll_lock.

These figure properties provide additional information about the user's interaction with the window: CurrentCharacter, SelectionType, and CurrentPoint.

You can interrupt the waitforbuttonpress function by typing Ctrl+C, but it returns an error unless your code calls it from within a try/catch block. The waitforbuttonpress function errors if the user closes the figure by clicking the X (close box) unless your code calls the waitforbuttonpress function within a try/catch block.

Examples

These statements display text in the Command Window when the user either clicks a mouse button or types a key in the figure window:

f = figure;
w = waitforbuttonpress;
if w == 0
    disp('Button click')
else
    disp('Key press')
end
Was this topic helpful?