Effacer les filtres
Effacer les filtres

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

35 vues (au cours des 30 derniers jours)
Shae Morgan
Shae Morgan le 27 Oct 2015
Commenté : Walter Roberson le 26 Fév 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 commentaires
skynet2
skynet2 le 26 Fév 2017
Modifié(e) : skynet2 le 26 Fév 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 le 26 Fév 2017
Using strcmp() is critical for this situation.

Connectez-vous pour commenter.

Réponse acceptée

Chad Greene
Chad Greene le 27 Oct 2015
You may be able to use waitforbuttonpress.
  5 commentaires
Shae Morgan
Shae Morgan le 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 le 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'

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by