File Exchange

image thumbnail

getkeywait

version 3.0 (2.18 KB) by Jos (10584)
Wait a certain time for a single keypress

5 Downloads

Updated 11 Feb 2019

View Version History

View License

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.
Without argument, getkeywait waits until a key is pressed.

[CH, RT] = getkeywait(..) returns the response time in seconds in RT.


Example:
disp('Press a key within 5 seconds') ;
[CH, DT] = getkeywait(5)

See also input, ginput, waitbar, msgbox
getkey (file Exchange)

Authors note: This file was created in 2005 and is still working ;-)

Cite As

Jos (10584) (2021). getkeywait (https://www.mathworks.com/matlabcentral/fileexchange/8297-getkeywait), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (21)

Jos (10584)

@Ben, no it does not as this was created to use it in the command window of matlab :-)

Ben Kieniewicz

does not work for a compiled program

Wessel van der Ham

@ Jos - thanks for the super fast reply even after all those years! I'll try to work it out in the way you suggested. Thanks a lot!

Jos (10584)

@Wessel - My function is intended for command window-based user interaction in matlab. When you use the PsychoToolbox, I recommend to use it's own builtin functions that asks for user input (like kbCheck?). [or you can ask the developers of that toolbox to fix their interference problems with matlab figures ;-) ]

Wessel van der Ham

Heya! I'm using a script that heavily relies on the 'Screen' function in PsychToolbox. I need to have a period where a user can take a break until a keypress, or until 5 minutes have passed (your function is ofcourse perfect for this). However, your function relies on a figure and the uiwait command both of which interfere with the 'Screen' function I believe. I tried to comment out certain parts but either the 'Screen' function is interrupted as another window is opened, or the function no longer works. Have you got any idea as to whether or not this is fixable? Thanks!

Chang hsiung

Lukas Meyers

Feng Ling

Michael Bronstein

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;
set(fh,...
'name','Press a key', ...
'keypressfcn',callstr, ...
'numbertitle','off', ...
'userdata',-1);

alex

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

alex

Jos (10584)

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

Sanchito

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?

thanks

Jesse Hopkins

Just what I was looking for

Ehsan Negahbani

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.

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: getkey

Inspired: waitinput

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!