Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to capture keypress of function/control keys from m-file

239 views
Skip to first unread message

ro...@nospam.com

unread,
Jul 12, 2005, 11:35:26 AM7/12/05
to
Hi

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

Brett Shoelson

unread,
Jul 12, 2005, 11:55:16 AM7/12/05
to

<ro...@nospam.com> wrote in message news:42d3...@news.eftel.com...

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]))


ro...@nospam.com

unread,
Jul 12, 2005, 6:05:57 PM7/12/05
to
"Brett Shoelson" <shoelson...@removethis.helix.nih.gov> wrote:

>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.


Brett Shoelson

unread,
Jul 12, 2005, 8:29:30 PM7/12/05
to
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?
Can you give an example of what you are trying to do?
Brett

ro...@nospam.com

unread,
Jul 12, 2005, 11:35:32 PM7/12/05
to
"Brett Shoelson" <shoel...@helix.spam.gov> wrote:

>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

Ann Thresher

unread,
Oct 14, 2005, 8:30:27 AM10/14/05
to
Hi, Guys,

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.

Jos

unread,
Oct 14, 2005, 9:04:24 AM10/14/05
to

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

Jason S

unread,
Oct 19, 2005, 12:13:05 PM10/19/05
to
I also need to do this. The getkey() function from Jos seems to work
well, though it's very silly that one would have to resort to
squirrelly undocumented GUI features just to get a keypress.

(One would think the "pause" function would return a value.)

Jason S

unread,
Oct 19, 2005, 12:22:29 PM10/19/05
to
Actually, the GETKEY function cited has a problem, which is that it
takes mouse/keyboard focus away from the MATLAB command/editor windows.
Grr.

Somebody must have a MEX-function or other such wrapper for the C
getch() and/or kbhit() functions...

Jason S

unread,
Oct 19, 2005, 12:46:11 PM10/19/05
to
...any way to change getkey.m so it traps Ctrl-C and passes the right
exception along to MATLAB?

Jos

unread,
Oct 19, 2005, 1:43:02 PM10/19/05
to

I have to look into that ...
Jos (author of GETKEY)

Tamas H.

unread,
Nov 3, 2016, 6:20:08 AM11/3/16
to
Dear Max,

thank you for your reply. I am also looking for an easy way to a) detect if a key was pressed (without waiting) and b) to get special keystrokes into MATLAB c) without a GUI.

I looked into Psychtoolbox, but it is way too heavy for what I am looking for. I tried to find the relevant parts in KbCheck and getchar, but that's also not trivial. In my case (and I guess in the case of most users wanting the features I enumerated above) the environment is MATLAB on Windows 7+ with Java available.

Thanks,
Tamas

"Maximilien Chaumon" wrote in message <n2g102$eee$1...@newscl01ah.mathworks.com>...
> Hey guys,
> You want to use the Psychtoolbox, which has a suite of tools to capture millisecond precision key presses to perform psychology experiments. the tools you're looking for are getchar (if you're intrerrested in characters) and KbCheck and other tools if you're interested in timing.
> Enjoy!
> Max
>
> Jos <x@y.z> wrote in message <ef0d...@webx.raydaftYaTP>...

Florian Enner

unread,
Jan 26, 2017, 5:28:08 PM1/26/17
to
"Tamas H." <new...@freemail.hu> wrote in message <nvf30j$8k1$1...@newscl01ah.mathworks.com>...
I recently submitted MatlabInput (https://mathworks.com/matlabcentral/fileexchange/61306-hebirobotics-matlabinput) ). It provides a non-blocking way to retrieve a snapshot of the current state of a keyboard - similar to the way vrjoystick works.

It's a really simple wrapper around JInput that doesn't rely on undocumented MATLAB features and doesn't require any particular windows to be in focus.

Example:

kb = HebiKeyboard();
while true

% Do something when ESC is pressed at the same time as buttons 'a' and '0'
state = read(kb);
if state.ESC && all(state.keys('a0'))
% do something
end

end

The source code is at https://github.com/HebiRobotics/MatlabInput

Hope it helps.

Florian

Srikar Bollavaram

unread,
Feb 25, 2017, 4:01:13 PM2/25/17
to
"Florian Enner" wrote in message <o6dt5j$ps6$1...@newscl01ah.mathworks.com>...
Hello Florian,
I apologize if this is a basic question but I keep getting this warning when I try implementing HebiKeyboard:

Warning: Invalid file or directory 'C:\Users\...\MatlabInput-master\src\main\resources\matlab\%RELEASE_NAME%.jar'.

Is there anything else I have to do to install it other than just unzip and add the paths to MATLAB?

Florian Enner

unread,
Feb 26, 2017, 2:01:13 PM2/26/17
to
"Srikar Bollavaram" wrote in message <o8sraf$3v2$1...@newscl01ah.mathworks.com>...
It looks like you downloaded the raw source code, which you'd need to build first. You can download pre-built releases from https://github.com/HebiRobotics/MatlabInput/releases

Just unzip the latest release file (zip), add the resulting folder to your path, and instantiate it.

Florian
0 new messages