How can I pause until the "return" key is pressed?

56 views (last 30 days)
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
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
Walter Roberson
Walter Roberson on 26 Feb 2017
Using strcmp() is critical for this situation.

Sign in to comment.

Accepted Answer

Chad Greene
Chad Greene on 27 Oct 2015
You may be able to use waitforbuttonpress.
  5 Comments
Shae Morgan
Shae Morgan on 30 Oct 2015
The return key is working fine, the problem is that the program is also registering "shift" as "return". while the program is waiting, if the user is typing into the edit-text box and uses a capital letter (by pressing shift) the program moves on to the next command instead of waiting exclusively for "return".
still stuck.
Walter Roberson
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'

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!