How to determine if enter(return) was pressed ?

조회 수: 4 (최근 30일)
Petr
Petr 2012년 11월 6일
Hi,
in one part of my code I need to determine if enter(return) was pressed. I am useing simple part of code:
kkey = get(gcf,'CurrentCharacter');
if strcmp(kkey, %comparing parameter)
% my code
end
which works very well for every key imaginable except return key...when I try to examine it step by step I found that if I press for example number 1:
kkey = '1'
but when I press return field is suddenly 1x1char and blank...why is that ? shouldnt kkey variable be '13' like in the ascii that coresponds to return ??
From my point of view it's wierd...How can I than compare it ??
Thanks, Peter

채택된 답변

Petr
Petr 2012년 11월 7일
Solution was simple and enough elegant for me...
.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
key = get(gcf,'CurrentKey');
if(strcmp (key , 'return'))
pushbutton1_Callback(hObject, eventdata, handles)
end
function pushbutton1_Callback(hObject, eventdata, handles)
%code to be executed

추가 답변 (1개)

Sven
Sven 2012년 11월 6일
편집: Sven 2012년 11월 6일
Hi Peter,
I've found that it's often easier to supply an onKeyPress function like follows:
function testme()
figure('KeyPressFcn', @(src,evnt)onKeyPressRelease(evnt,'press'),...
'KeyReleaseFcn',@(obj,evnt)onKeyPressRelease(evnt,'release')...
);
end
function onKeyPressRelease(evnt, pressRelease)
disp(evnt)
disp(pressRelease)
end
If you test this, you'll see that you can capture "return" in the event.Key property:
Character: '
'
Modifier: {1x0 cell}
Key: 'return'
And you will know whether it was just pressed or released.
Thanks, Sven.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by