4

Do you know how to read keyboard strokes into Matlab while a Matlab gui is running? (I.e., without using the "input" function which sends a prompt to the command window and needs you to press return).

We would like to avoid using a mex function if possible.

learnvst
  • 15,455
  • 16
  • 74
  • 121

3 Answers3

9

You would first have to declare your figure by handle:

fig = figure;

then you can set properties (in quotes below) to activate functions that you've written to respond to user interactions (with the @ signs):

set(fig,'KeyPressFcn',@keyDownListener)
set(fig, 'KeyReleaseFcn', @keyUpListener);
set(fig,'WindowButtonDownFcn', @mouseDownListener);
set(fig,'WindowButtonUpFcn', @mouseUpListener);
set(fig,'WindowButtonMotionFcn', @mouseMoveListener);

The above example is from shooter03.m a MATLAB space shooter, an excellent source (from the matlab file exchange) for many aspects of user object interaction in MATLAB:

http://www.mathworks.com/matlabcentral/fileexchange/31330-daves-matlab-shooter/content/shooter03/shooter03.m

Keegan Keplinger
  • 627
  • 5
  • 15
3

If your GUI is based on a figure, you can use the figure property keypressfcn to define a callback function that handles keyboard inputs. See the matlab help for further descriptions: http://www.mathworks.de/help/techdoc/ref/figure_props.html#KeyPressFcn

H.Muster
  • 9,297
  • 1
  • 35
  • 46
2

Try:

hf = figure;
get(hf,'CurrentCharacter')
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
JasonK
  • 21
  • 1