How do I prevent two simultaneous key presses from tabbing away from my GUI?

조회 수: 6 (최근 30일)
I have a gui that allows people to listen to a sentence and then type what they think the sentence said. The problem is, if someone is typing too fast and accidentally presses two keys at the same time, instead of putting both characters in the edit box, it tabs out and goes to the command line in MatLab. How do I prevent this from happening? I'm using the following code to pause while the person types their response until "enter" gets pressed: Any ideas would be helpful
currkey=0;
% do not move on until enter key is pressed
while currkey~=13 %13 is the ascii number for the return character
pause; % wait for a keypress
currChar=get(gcf,'CurrentCharacter');
currkey = double(currChar);
if isempty(currkey)
currkey=0;
else
end
end
Any ideas?

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 1일
Use a WindowKeyPressFcn callback in the figure instead.
  댓글 수: 1
Shae Morgan
Shae Morgan 2015년 12월 8일
편집: Shae Morgan 2015년 12월 8일
Tips on how to do this? I've never used WindowKeyPressFcn before...also, I am using GUIDE, so will that mess up my handles structure? I'm fairly new to MatLab so any help is appreciated!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

valdal
valdal 2015년 12월 1일
편집: valdal 2015년 12월 1일
With KeyPressFcn and guidata :
h = uicontrol(...);
escape = false
guidata(h,escape)
set(h, 'KeyPressFcn', @key_pressed);
while ~escape
pause(0.01);
escape = guidata(h)
end
function key_pressed(h, e)
if strcmp(e.Key, 'return')
guidata(h, true)
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 12월 1일
Warning: using guidata() like that will destroy your handles structure if you are using GUIDE.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by