I have code that contains the following repeating pattern:
- Ask the user to press ⏎ to begin computation (using
input
). - Perform some lengthy computation and display some info to the user.
- Wait for the user to press ⏎ again to proceed to further computations.
The problem I am facing: if the user presses Return/Enter/ ⏎ more than once after the 1st prompt appears (presumably by mistake), these presses are intercepted by the following prompts which causes the rest of the code to execute.
The desired behavior: I would like to discard any number of keyboard events happening after the acceptance of the a prompt and before the appearance of the following one, such that the computation does not proceed without explicit user interaction with each prompt.
Code that reproduces the problem:
function q34593155()
%% // Init
clc;
import java.awt.Robot; import java.awt.event.*; robot = Robot;
%% // 1st prompt:
disp('--- Some initial info the user should see ---');
[~] = input('\nPress "Return" to start phase 1.\n','s');
disp('Please wait while computation is running...');
%// Here we simulate an additional press:
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
pause(2);
disp('--- results of the 1st part ---');
%% // 2nd prompt:
[~] = input('\nPress "Return" to start phase 2.\n','s');
disp('Please wait while computation is running...');
%// ^ Should only happen after another explicit press on "Enter"');
pause(2);
disp('--- results of the 2nd part ---');
Instructions: either press "Enter" once on the 1st prompt, or comment out robot.keyPress
and robot.keyRelease
and press twice (or more).