Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Edit console input

Hey there, is there any way to edit the console input before the user presses the "enter" key?

what I mean is for example a loop that constantly checks what the user has typed in, and if the text is BYE then it replaces it with goodbye.

this has to be done before the user submits the text, so for example this

if(console.readline() == "bye")
{
console.WriteLine("goodbye");
}

wont be accepted as a solution
0
freefrag
Asked:
freefrag
  • 3
  • 2
1 Solution
 
utter77Commented:
Hi.

You can use the "console.readKey()" along with "console.SetCursorPosition()" to acomplish that.

/MAtt
0
 
Todd GerbertIT ConsultantCommented:
You again, eh? haha...

Yes, you can do that BUT you need to use Console.ReadKey(true)  to read a character at a time, without it being shown, and handle a few things yourself - like checking whether what's entered so far matches one of the keywords such as "bye", or if the user pressed enter, or backspace, etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.Write("Enter a string: ");
			string userInput = MyReadLine();
			Console.WriteLine("You typed: " + userInput);
			Console.WriteLine("Press any key to exit...");
			Console.ReadKey();
		}
 
		static string MyReadLine()
		{
			StringBuilder userText = new StringBuilder();
 
			while (true)
			{
				if (Console.KeyAvailable)
				{
					char keyPress = Console.ReadKey(true).KeyChar;
 
					switch (keyPress)
					{
						case '\r':
						case '\n':
							Console.Write(Environment.NewLine);
							return userText.ToString();
						case '\b':
							userText.Remove(userText.Length - 1, 1);
							break;
						default:
							userText.Append(keyPress);
 
							if (userText.ToString() == "bye")
							{
								userText.Remove(0, 3);
								userText.Append("goodbye");
 
								Console.SetCursorPosition(Console.CursorLeft - 2, Console.CursorTop);
								Console.Write("goodbye");
							}
							else
							{
								Console.Write(keyPress);
							}
							break;
					}
				}
			}
		}
	}
}

Open in new window

0
 
freefragAuthor Commented:
hah :), problem with that is that you cant remove the written text
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Todd GerbertIT ConsultantCommented:
You can, I just forgot a couple things in the case of backspace,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.Write("Enter a string: ");
			string userInput = MyReadLine();
			Console.WriteLine("You typed: " + userInput);
			Console.WriteLine("Press any key to exit...");
			Console.ReadKey();
		}
 
		static string MyReadLine()
		{
			StringBuilder userText = new StringBuilder();
 
			while (true)
			{
				if (Console.KeyAvailable)
				{
					char keyPress = Console.ReadKey(true).KeyChar;
 
					switch (keyPress)
					{
						case '\r':
						case '\n':
							Console.Write(Environment.NewLine);
							return userText.ToString();
						case '\b':
							userText.Remove(userText.Length - 1, 1);
							Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
							Console.Write(" ");
							Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
							break;
						default:
							userText.Append(keyPress);
 
							if (userText.ToString() == "bye")
							{
								userText.Remove(0, 3);
								userText.Append("goodbye");
 
								Console.SetCursorPosition(Console.CursorLeft - 2, Console.CursorTop);
								Console.Write("goodbye");
							}
							else
							{
								Console.Write(keyPress);
							}
							break;
					}
				}
			}
		}
	}
}

Open in new window

0
 
freefragAuthor Commented:
thats exactly what I needed :P

I wont be bothering you anymore (for a while at least)
0
 
Todd GerbertIT ConsultantCommented:
No problem, glad to help.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now