[转载] http://blog.sina.com.cn/s/blog_6163bdeb0100n9cq.html
常用的有两个WindowKeyPressFcn和WindowKeyReleaseFcn,分别表示按下按键和释放按键的响应函数,两者调用形式上是一样的。
1、说明
以indowKeyPressFcn为例,按键响应无论当前焦点在figure上还是控件上,这一点和鼠标响应有所不同。它的响应函数需要至少两个参数,即发生按键的figure和event结构体。
even结构中包含了按键的各种信息,如下图

其中含义如下
- Character表示按键实际会显示的字符,如shift+k,则此值为K,只按下shift,则此值为空;
- Modifier是一个元胞类型,包含所按所有修饰键的名字,如按下ctrl+shift+a,则此值为{‘shift’ ,’control’};
- Key表示按键的名称,如k,a,multiply,delete等。
2、简单的测试程序
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
%% 测试windowkeypressfcn响应函数
function keypress_test
clc
close all
clear all
%%
fig = figure;
plot(1:10)
set(fig,'windowkeypressfcn',@keypressfcn);
set(fig,'windowkeyreleasefcn',@keyreleasefcn);
function keypressfcn(h,evt)
fprintf('************press n');
evt
fprintf('************ n');
end
function keyreleasefcn(h,evt)
fprintf('************release n');
evt
fprintf('************ n');
end
end
要注意,由于不可能同时按下两个键,所以多按键是挨个响应的。如按下ctrl+shift+a时,输出打印如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
************press
evt =
Character: ''
Modifier: {'control'}
Key: 'control'
************
************press
evt =
Character: ''
Modifier: {'shift' 'control'}
Key: 'shift'
************
************press
evt =
Character: ''
Modifier: {'shift' 'control'}
Key: 'a'
************
************release
evt =
Character: ''
Modifier: {'shift' 'control'}
Key: 'a'
************
************release
evt =
Character: ''
Modifier: {'control'}
Key: 'shift'
************
************release
evt =
Character: ''
Modifier: {1x0 cell}
Key: 'control'
************
可知按下顺序为control—shift—a,释放顺序为a—shift—control。
3、通过按下按键,从停止位置继续运行
gcf是Current figure handle,调用get函数得到curent figure的CurrentCharacter属性。判断是否按下e键,从而从while中跳出去继续运行。程序如下所示
复制代码
1
2
3
4
5
6
7
8
figure;
pause(1);
while 1
pause(0.1)
if strcmpi(get(gcf,'CurrentCharacter'),'e')
break;
end
end
最后
以上就是落寞云朵最近收集整理的关于matlab中figure对象的按键响应的全部内容,更多相关matlab中figure对象内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复