matlab等待按键响应,matlab中figure对象的按键响应
?
常用的有兩個WindowKeyPressFcn和WindowKeyReleaseFcn,分別表示按下按鍵和釋放按鍵的響應(yīng)函數(shù),兩者調(diào)用形式上是一樣的。
1、說明
以indowKeyPressFcn為例,按鍵響應(yīng)無論當前焦點在figure上還是控件上,這一點和鼠標響應(yīng)有所不同。它的響應(yīng)函數(shù)需要至少兩個參數(shù),即發(fā)生按鍵的figure和event結(jié)構(gòu)體。
even結(jié)構(gòu)中包含了按鍵的各種信息,如下圖
其中含義如下
Character表示按鍵實際會顯示的字符,如shift+k,則此值為K,只按下shift,則此值為空;
Modifier是一個元胞類型,包含所按所有修飾鍵的名字,如按下ctrl+shift+a,則此值為{‘shift’ ,’control’};
Key表示按鍵的名稱,如k,a,multiply,delete等。
2、簡單的測試程序
%% 測試windowkeypressfcn響應(yīng)函數(shù)
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
要注意,由于不可能同時按下兩個鍵,所以多按鍵是挨個響應(yīng)的。如按下ctrl+shift+a時,輸出打印如下
************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、通過按下按鍵,從停止位置繼續(xù)運行
gcf是Current figure handle,調(diào)用get函數(shù)得到curent figure的CurrentCharacter屬性。判斷是否按下e鍵,從而從while中跳出去繼續(xù)運行。程序如下所示
figure;
pause(1);
while 1
pause(0.1)
if strcmpi(get(gcf,'CurrentCharacter'),'e')
break;
end
end
?
總結(jié)
以上是生活随笔為你收集整理的matlab等待按键响应,matlab中figure对象的按键响应的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《秋暮登北楼》王武陵
- 下一篇: 一线大厂青睐的前端人,90%满足这3个条