Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

waitforbuttonpress

Wait for key press or mouse-button click

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

Here are some important points to consider when using the waitforbuttonpress function:

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

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

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

  • 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.

  • 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.

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

Introduced before R2006a

Was this topic helpful?