MATLAB高级图形用户界面与图形绘制技巧
1. 键盘输入响应
在某些情况下,用户通过键盘来控制程序的操作可能会更方便。在图形窗口中,有一个名为 KeyPressFcn 的属性。当将该属性的值设置为一个函数句柄时,只要按下键盘上的任意键,对应的函数就会被执行。
下面是一个示例代码,展示了如何从传递给按键处理函数的事件结构 evnt 中提取用户按下的键的信息:
function DemoKeyPress
figure(1);
set(gcf, 'KeyPressFcn', @processKey);
% Note: make figure the active window by clicking on it
function processKey(src, evnt)
if length(evnt.Character)>=1
clc;
disp(['Character: ', evnt.Character]);
disp(['Modifiers: ', ca2str(evnt.Modifier)]);
disp(['Key: ', evnt.Key]);
end
end
function cas=ca2str(ca)
% convert 1xN cell array of strings to a single
% string with space
MATLAB高级GUI与图形绘制
超级会员免费看
订阅专栏 解锁全文
1431

被折叠的 条评论
为什么被折叠?



