How can I pause until the "return" key is pressed?
35 views (last 30 days)
Show older comments
Shae Morgan
on 27 Oct 2015
Commented: Walter Roberson
on 26 Feb 2017
I have a gui that I need to have wait until the return key is pressed before advancing. Below is my code but I keep getting an error saying:
Error using == Matrix dimensions must agree.
Error in RunDlg_bab>ok_Callback (line 388) if currkey=='return'
Below is my code:
currkey=0;
% do not move on until enter key is pressed
while currkey~=1
pause; % wait for a keypress
currkey=get(gcf,'CurrentKey');
if currkey=='return'
currkey==1
else
currkey==0
end
end
Any sort of help would be really appreciated!
2 Comments
skynet2
on 26 Feb 2017
Edited: skynet2
on 26 Feb 2017
This looks fine, except you accidentally are checking for equality when you want to set currKey to 1 or 0. Revision follows:
currkey=0;
% do not move on until enter key is pressed
while currkey~=1
pause; % wait for a keypress
currkey=get(gcf,'CurrentKey');
if strcmp(currkey, 'return') % You also want to use strcmp here.
currkey=1 % Error was here; the "==" should be "="
else
currkey=0 % Error was here; the "==" should be "="
end
end
Accepted Answer
Chad Greene
on 27 Oct 2015
5 Comments
Walter Roberson
on 30 Oct 2015
If the purpose is to wait until return is pressed, then do not use waitforbuttonpress at all. Instead, once it is determined that the mode should be in place, waitfor() or uiwait() a condition that is made true by your Callback on the uicontrol, as that Callback will not be activated until Return is pressed. There would be no need to monitor every key.
CurrentKey is the name of the key that was pressed, which might include 'shift' . If an upper-case character was chosen then the name of the key is the lower case version of it
CurrentCharacter is [] (empty) is there no value associated with the key that was typed, such as is the case for shift. Otherwise it is the char() of what was typed, so for control-f it would be char(6) and for shift-f it would be char(70) which is 'F'
More Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!