Engaged in the creation of a console emulator. The input field is made as a TextBox and the commands are processed by the textbox PreviewKeyDown event (by pressing Enter). After processing, the command should produce a string result and output it to the textBox. All would be nothing, if it were not necessary to create a type of command that requires additional user input, for example, some commands must require certain privileges hidden by the admin password.
Here is a simplified view of what I'm trying to do:
private string expectUserInput( string inviteString ) { CLI_TextBox.Text += "\r\n" + inviteString; CLI_TextBox.CaretIndex = CLI_TextBox.Text.Length; return "some result"; } private string enable( ) { string password = expectUserInput("Password: "); // If password is correct - do some staff; return "someStaffResult"; } private void CLI_TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { string result; string lineContent = CLI_TextBox.GetLineText(CLI_TextBox.GetLastVisibleLineIndex()); if (lineContent == "enable") { result = enable(); } } else if (e.Key == Key.Up || e.Key == Key.Down) { e.Handled = true; } } My idea (well, or at least a hypothetical idea) is to make the expectUserInput () function as if waiting for a specific event (in my case, again, pressing the Enter key, but the truth is, this event already has its handler) and until this event comes, it will not be executed, and enable () will not be executed. I shoveled Google, but I did not find a solution. I understood only that I need to dig in the direction of async / await and generate my own events (maybe), but I don’t quite understand how to apply it in this situation. Perhaps someone faced with a similar problem and found a successful solution?
int mode=0;. all clicks are processed in CLI_TextBox_PreviewKeyDown. if mode == 0, then process everything as usual. but if additional input is required, then setmode=1, and in this mode you are waiting for special presses. as soon as you get it, go tomode=0or some other. - Stackenable()function to start until theexpectUserInput()function isexpectUserInput(), which, in turn, is also not executed until a certain event occurs. - Ivan Frolov