Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp2
- {
- class Program
- {
- private static bool wasEscapePressed = false;
- static void Main(string[] args)
- {
- Console.WriteLine("Press ESC at any time to exit the loop");
- while (!wasEscapePressed)
- {
- Console.Write("Enter your name: ");
- string name = ReadLineEx(key =>
- {
- if (key.Key == ConsoleKey.Escape)
- {
- wasEscapePressed = true;
- return true;
- }
- if (key.Key == ConsoleKey.Enter)
- {
- return true;
- }
- return false;
- });
- if (wasEscapePressed)
- {
- Console.WriteLine("ESCAPE PRESSED!!!");
- }
- else
- {
- Console.WriteLine("You said your name is " + name);
- }
- }
- Console.WriteLine("Press any key to exit");
- Console.ReadKey();
- }
- static string ReadLineEx(Func<ConsoleKeyInfo, bool> isDoneCallback)
- {
- string input = "";
- bool isDone = false;
- wasEscapePressed = false;
- while (!isDone)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- isDone = isDoneCallback(key);
- if (!isDone)
- {
- input += key.KeyChar;
- Console.Write(key.KeyChar);
- }
- }
- Console.WriteLine();
- return input;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment