Matlab docs say there are 3 ways to get keypress values:
(1) input() [but it only works for printable text or digits]
(2) manual input during pause()
(3) "build a complete GUI".
None of these cover the fairly common situation where you want to detect
specific key or keys (eg, ESC or hot key combinations) with an m-file
statement. Most languages have a 'keypress' function of some type which can
get any pressed key values and distinguish between them. Matlab doesn't seem
to - have I missed it? I can't believe you have to build a GUI just for this.
thanks
ross
Do a search of the online docs...you'll find that figures and uicontrols
have a keypressfcn value. You should be able to use these to do just about
anything you need to do.
Brett
--
char(cumsum(...
[32 83 -11 7 -10 7 7 -4 -1 -46 40 -3 7 -3 15 -74 64 -5 -1 -58 57 8 7]))
>Do a search of the online docs...you'll find that figures and uicontrols
>have a keypressfcn value. You should be able to use these to do just about
>anything you need to do.
>Brett
>--
>char(cumsum(...
>[32 83 -11 7 -10 7 7 -4 -1 -46 40 -3 7 -3 15 -74 64 -5 -1 -58 57 8 7]))
Thanks for the comment. I had found keypressfcn, but thought it was something
of a pig's ear. A lot of my programs prompt for parameters then do file i/o
and number crunching. Not a ui control or figure in sight. Opening a figure to
use its keypressfcn is such a mucky & inefficient way to do it (espec when
'getkey' and equivalents have been ubiquitous since the early 70s).
By the way, I really like your way of munging your email address - very neat!
ross.
>So you're looking for a way to capture a keypress at the command
>line, without using input? Hmmm. Why no input? Does KEYBOARD help?
Let's say you set up a loop like so (in pseudocode):
while <forever>
get key press
print char & also ASCII value
wend
then in the half-dozen or so programming languages that I've used since 1974,
you could do this and get a sensible answer from every key. For example,
key resulting display
------- ----------------------
'a' a 97 or 0x61
! ! 33 or 0x21
ESC 27 or 0x1B
F3 an escape sequence, a DOS dual-register syscall return,
or some other unique ID
This was useful in many ways. However in Matlab, input() allows only digits,
or with 2nd arg = 's', only printing chars. Function keys, ESC and hot key
combinations are just ignored. This is just stupid, IMO. Eg, you cannot set
the possible replies to a prompt as ['y' | 'n' | <return> | <escape>] or [F1
| F2 | F3 ]. Why not? On many systems they make more sense than arbitrary
letter equivalents. In fact that's why they were added to the keyboard.
Other possibilities:
Keyboard() is no use - it puts the m file into debug mode, which has to be
manually cancelled before excecution resumes. Streams with FIDs 1 & 2 are
predefined in Matlab, but 0 is not, so I can't sneak in with fscanf(0, ...) or
equivalent. I have no other reason to open a UI control, and I'm not sure
what they'll recognise anyway, since I haven't used them in Matlab. Write a C
or assembly routine and interface it to Matlab? In theory I could, but I
haven't had an assembler or C compiler on my PC for a long time.
All I want for Christmas is a function which recognises the ESC key. Yes, no,
maybe?
thanks
ross
I just saw this message and it's exactly what I need to do (which is why I looked in the first place). Except I need to use all the function keys, the escape key, the page up/down keys, delete,home and end keys and all the number pad keys as distinct from the keyboard number keys, etc. As Ross said, this is easy in fortran but I can't see any way to use these keys in matlab (yet...). I'm programming a gui so getting the id of normal keys is not problem. It's the extras that are giving me a headache.
Any suggestions would be welcome!
Thanks.
Ann.
Take a look at my GETKEY function at the file exchange:
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7465&objectType=FILE>
It might be what you're looking for.
hth
Jos
(One would think the "pause" function would return a value.)
Somebody must have a MEX-function or other such wrapper for the C
getch() and/or kbhit() functions...
I have to look into that ...
Jos (author of GETKEY)