Documentation Center

  • Trial Software
  • Product Updates


Wait for key press or mouse-button click


k = waitforbuttonpress


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 function returns

  • 0 if it detects a mouse button click

  • 1 if it detects a key press

If a WindowButtonDownFcn 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.

Additional information about the event that causes execution to resume is available through the figure CurrentCharacter, SelectionType, and CurrentPoint properties.

You can interrupt waitforbuttonpress by typing Ctrl+C, but an error results unless the function is called from within a try/catch block. You also receive an error from waitforbuttonpress if you close the figure by clicking the X close box unless you call waitforbuttonpress within a try/catch block.


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

w = waitforbuttonpress;
if w == 0
    disp('Button click')
    disp('Key press')

See Also

| | |

Was this topic helpful?