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

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

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
Using strcmp() is critical for this situation.

Sign in to comment.

 Accepted Answer

You may be able to use waitforbuttonpress.

5 Comments

Yeah it doesn't work so well, because while paused, the user is typing input into a text edit box. So I'm trying to allow the text edit to have answers typed into it, but the whole gui doesn't move on until specifically the return key (enter) is pressed. Any other ideas?
The Callback function of a uicontrol style 'text' does not get activated until return is pressed or the user selects a different control.
I'm not sure what the difference is between 'CurrentKey' and 'CurrentCharacter', but when I've done this I've used the following syntax:
while true
w = waitforbuttonpress;
switch w
case 1 % (keyboard press)
key = get(gcf,'currentcharacter');
switch key
case 116 % 116 is lowercase t
disp('Have some tea.')
case 27 % 27 is the escape key
disp('User pressed the escape key. I''m quitting.')
break % break out of the while loop
case 13 % 13 is the return key
disp('User pressed the return key. Quitting the loop.')
break
otherwise
% Wait for a different command.
end
end
end
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.
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

Community Treasure Hunt

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

Start Hunting!