How to request user input to press Esc or Enter ?

12 ビュー (過去 30 日間)
Sim
Sim 2022 年 12 月 13 日
コメント済み: Sim 2022 年 12 月 13 日
How to request user input to press Esc or Enter ?
% this is a pseudo-code I wrote just to give an idea about what I am trying to achieve:
answer = input('Please enter Esc to exit this code or Enter to continue:');
keepRunning = true;
while (keepRunning)
if (answer ~= Esc && answer ~= Enter) % If the user does not input either Esc or Enter
answer = input('ERROR. Please enter Esc to exit this code or Enter to continue:');
elseif (answer == Esc) % Exit this code
disp('exit the code')
keepRunning = false;
return
elseif (answer == Enter) % Continue this code and do stuff
disp('continue to run this code')
keepRunning = false;
continue % or do stuff here
end
end

採用された回答

Jan
Jan 2022 年 12 月 13 日
編集済み: Jan 2022 年 12 月 13 日
This will not work. The input command is not sufficient to catch a pressed Esc key.
Using the input capabilities of the command window to interprete key clicks is a bad choice in general. Use a figure instead, which is nicer and allows to analyse the pressed keys using the WindowKeyPressFcn and even the WindowKeyReleaseFcn.
The command window is not powerful enough to solve such genereal purpose GUI jobs. You can do it using the undocumented Java interface, but this is fragile.
  1 件のコメント
Sim
Sim 2022 年 12 月 13 日
Great answer @Jan!! Thanks for showing the possible ways to take :-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by