1

I'm trying to make a KeyDown statement work. So I'm writing:

private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e);

System.Windows.Forms won't work I read that its a Visual Studio Code thing you have to go to Project.json and add System.Windows.Forms as a dependency. I don't know what to write to add it. I searched the web and searched stock overflow. I can't find anything.

I don't know what to type in to add it.

Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
Bob
  • 15
  • 5
  • What are you trying to achieve with `KeyDown`? That is an event method that exists in the Windows Forms controls. If you are wanting to access KeyDown events in a Console app, you have to build your own mechanic for it – Johnathon Sullinger Jul 28 '16 at 19:52
  • I want to make it watch what key you type in with KeyDown so that your command line, where you type stuff in at, will turn green when you type in a number. – Bob Jul 28 '16 at 19:55

1 Answers1

0

I could be wrong, but I don't believe you can use the System.Windows.Forms assembly with a .Net Core project. My Visual Studio is acting up so I wasn't able to try using it via the project.json imports feature. Having said that, it wouldn't provide you with what you want anyway.

Since you are wanting to capture the input from the user, via the console, and change the color based on some conditions - you'll have to do that manually yourself.

The following is a complete application example that shows how to do that. Essentially you have to evaluate each character entered into the console and determine if it's a number. If it is a number, you have to move the cursor back 1 position so you can overwrite the value that was just entered. Prior to overwriting the value, you change the consoles foreground color.

using System;
namespace ConsoleApp2
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Set up an infinite loop that will allow us to forever type unless 'Q' is pressed.
            while(true)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey();

                // If Q is pressed, we quit the app by ending the loop.
                if (pressedKey.Key == ConsoleKey.Q)
                {
                    break;
                }

                // Handle the pressed key character.
                OnKeyDown(pressedKey.KeyChar);
            }
        }

        private static void OnKeyDown(char key)
        {
            int number;

            // Try to parse the key into a number.
            // If it fails to parse, then we abort and listen for the next key.
            // It will fail if anything other than a number was entered since integers can only store whole numbers.
            if (!int.TryParse(key.ToString(), out number))
            {
                return;
            }

            // If we get here, then the user entered a number.
            // Apply our logic for handling numbers
            ChangeColorOfPreviousCharacters(ConsoleColor.Green, key.ToString());
        }

        private static void ChangeColorOfPreviousCharacters(ConsoleColor color, string originalValue)
        {
            // Store the original foreground color of the text so we can revert back to it later.
            ConsoleColor originalColor = Console.ForegroundColor;

            // Move the cursor on the console to the left 1 character so we overwrite the character previously entered
            // with a new character that has the updated foreground color applied.
            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
            Console.ForegroundColor = color;

            // Re-write the original character back out, now with the "Green" color.
            Console.Write(originalValue);

            // Reset the consoles foreground color back to what ever it was originally. In this case, white.
            Console.ForegroundColor = originalColor;
        }
    }
}

The output to the console will look like this:

Result

Community
  • 1
  • 1
Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102