File Exchange

image thumbnail


version 1.2 (2.02 KB) by

Wait a certain time for a single keypress (v2.1, jan 2012).

9 Ratings



View License

GETKEYWAIT - get a key within a time limit
CH = GETKEYWAIT(P) waits for a keypress for a maximum of P seconds. P should be a positive number. CH is a double representing the key pressed key as an ascii number, including backspace (8), space (32), enter (13), etc. If a non-ascii key (Ctrl, Alt, etc.) is pressed, CH will be NaN.
If no key is pressed within P seconds, -1 is returned, and if something went wrong during excution 0 is returned.

See also input,
getkey, stoploop, yesno (FileExchange)

Comments and Ratings (14)

Lukas Meyers

Feng Ling

This file is great. Thanks. One tip: If you do not want a new figure to be created, but still want to use this function, just substitute the following code for what is currently lines 41-47:
fh = gcf;
'name','Press a key', ...
'keypressfcn',callstr, ...
'numbertitle','off', ...


alex (view profile)

excellent,very nice work!
very useful to my project
thank you very much!


alex (view profile)

Jos (10584)

Jos (10584) (view profile)

@Sanchito: I suggest you implement the code/functionality of getkeywait into your own GUI directly.


when i am running this program it is making another figure wherein i need to press enter for it to continue.

what I am saying it is that: I have a gui wherein a person enters a node number and then wait for the enter press key.... and i say if ch== 13 then run the rest of the program but when i do press enter it opens a figure wherein i need to press enter again for me to continue the analysis. Is there anyway of me avioding this figure in the middle just directly analysing it as soon as enter is pressed?


Jesse Hopkins

Jesse Hopkins (view profile)

Just what I was looking for

Great. Thank you Jos.
Is it possible to prevent the appearance of figure tab in the task bar?
I'm calling your function in a loop every second, and it works well, but there is continuous blinking between command window and windows task bar as your code opens and closes a figure window each time. I tried to off the 'visible' property of the figure, but it was not working...

kk kk

thank you...fantastic work

Iohan Sandoval

very good job! thanks

Joseph Hollmann

Great submission

Inese Steinbahs

Thank You! This function worked out for my program wounderfully!

urs (us) schwarz

in need of some re-engineering as it creates a new timer object every time it is called, which could be deleterious when used in a long loop, for instance.



changed some figure properties; added check for figure existence


updated checks and output

Timer object was not deleted which could cause problems after (many) repetitive calls. Thanks to us.

Timer object was not deleted which could cause problems after (many) repetitive calls. Thanks to us.

MATLAB Release
MATLAB 6.5 (R13)

Inspired by: getkey

Inspired: waitinput

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today