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.
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 ;-)
Jos (10584) (2021). getkeywait (https://www.mathworks.com/matlabcentral/fileexchange/8297-getkeywait), MATLAB Central File Exchange. Retrieved .
@Ben, no it does not as this was created to use it in the command window of matlab :-)
does not work for a compiled program
@ 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!
@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 ;-) ]
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!
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', ...
excellent,very nice work!
very useful to my project
thank you very much!
@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?
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...
thank you...fantastic work
very good job! thanks
Thank You! This function worked out for my program wounderfully!
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.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!