如何在Matlab GUI运行时拦截按键输入

4
你知道怎么在Matlab运行时读取键盘输入吗?也就是说,不使用“input”函数(该函数会向命令窗口发送提示并需要你按回车键),希望避免使用Mex函数。
3个回答

9

首先,你需要通过句柄声明你的图形:

fig = figure;

然后,您可以设置属性(如下所示的引号)来激活您编写的响应用户交互的功能(使用 @ 符号):

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

上面的例子来自于一个名为shooter03.m的MATLAB空间射击游戏,它是MATLAB文件交换中心的一个优秀资源,涵盖了MATLAB用户对象交互的许多方面。请访问以下链接查看:http://www.mathworks.com/matlabcentral/fileexchange/31330-daves-matlab-shooter/content/shooter03/shooter03.m

3

2

尝试:

hf = figure;
get(hf,'CurrentCharacter')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接