4.5

4.5 | 6 ratings Rate this file 215 downloads (last 30 days) File Size: 2.02 KB File ID: #7465

getkey

by Jos

 

18 Apr 2005 (Updated 23 Apr 2009)

Code covered by BSD License  

Get a single keypress (v1.2 apr 2009)

Download Now | Watch this File

File Information
Description

GETKEY - get a single keypress

CH = GETKEY waits for a keypress and returns the ASCII code. Accepts all ascii characters, including backspace (8), space (32), enter (13), etc, that can be typed on the keyboard. Non-ascii keys (ctrl, alt, ..) return a NaN. CH is a double.
 
CH = GETKEY('non-ascii') uses non-documented matlab 6.5 features to return a string describing the key pressed. In this way keys like ctrl, alt, tab etc. can also distinguished. CH is a string.

This function is kind of a workaround for getch in C. It uses a modal, but non-visible window, which does show up in the taskbar.

Tested for ML versions 6.5 and up

Acknowledgements
This submission has inspired the following:
GETKEYNOW, YESNO, GetKeyWait
MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (11)
21 Apr 2005 Chris Brown

The INPUT function in R13 contains a bug that does not flush the keyboard buffer while within loops, meaning that after so many calls the keyboard will stop working.

As a result, getkey is especially useful to me. Thank you for making it available.

28 Apr 2005 Dimitri Shvorob

Chris, could you elaborate please? Running
'for i = 1:100, a = input('Enter a letter','s'), end', I didn't see any irregularities. (How many is so many?)
Jos, maybe you could write a companion function that outputs the key pressed now, rather than waits for one, as INPUT? I don't really know of any way - including via 'currentkey' figure property - to do that in Matlab, and a solution would be very helpful.
Thank you.

24 May 2005 George Aumann

Just what I was looking for.

31 Oct 2005 Patrick Craston

Great! been searching for something like this for ages..

02 Nov 2005 Erik Helberg  
02 Jan 2006 fred klugger

How can I use this?

06 Jan 2006 Jos van der Geest

to Fred Kluger: you may use this wherever you want a single key response from the user. You can use it in similar way as you would use <input> (which requires an additional "enter")

to Erik: ??

Jos

22 Apr 2008 Jay Vaughan

Did the job perfectly for me.

25 Apr 2008 Jens Rantil

This program is unnecvessary as it can be done using
[ignore,ignore2,keycode]=ginput(1);

27 Apr 2008 Jos (the author)

To Jens Rantil: GETKEY is not at all as trivial as your code implies. For instance, GINPUT needs an axis (and will create one if none exist) and can capture only a few keys.
GETKEY has none of this behavior, and is much more flexible as it can be used in codes without graphical output.

28 Jul 2008 gregor samsa

Good job. Worked for me

Please login to add a comment or rating.
Updates
03 Nov 2005

added some minor improvements

13 Dec 2006

updated lay-out

23 Apr 2009

tested for newer releases

Tag Activity for this File
Tag Applied By Date/Time
keypress Jos 22 Oct 2008 07:46:00
keyboard Jos 22 Oct 2008 07:46:00
input Jos 22 Oct 2008 07:46:00
key Jos 22 Oct 2008 07:46:00
kbhit Jos 22 Oct 2008 07:46:01
hit Jos 22 Oct 2008 07:46:01
getchar Jos 22 Oct 2008 07:46:01
read Jos 22 Oct 2008 07:46:01
getch Jos 22 Oct 2008 07:46:01
getkey Jos 23 Apr 2009 02:07:36
get Jos 23 Apr 2009 02:07:37
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com