user8516396
user8516396

Reputation:

detect key press

I have a code where I do something when a Key is pressed

if (Console.ReadKey(true).Key == ConsoleKey.G)
{
    Logger.Trace("Opening the GUI...");
}

How to detect if key is pressed by using character as A-B? I store shortcut letter in file and want to know if pressed but need to detect it by string and not ConsoleKey.

Upvotes: 0

Views: 96

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222720

You can use char.IsLetter() to check if its an alphabet

    ConsoleKeyInfo keyinfo;
    Console.ReadKey();
    while (!(Console.KeyAvailable  ))
    {
        keyinfo = Console.ReadKey();
        if (char.IsLetter(Console.ReadKey().KeyChar))
        {  
        }
    }

Upvotes: 2

Tobias Theel
Tobias Theel

Reputation: 3217

Save the Console.ReadKey result into a string variable and append further keystrokes and check with

if(inputString.Contains(key)){
  doSomething()
}

Have a look at the following answers: https://stackoverflow.com/a/16037492/4992212

Detect when two keys are pressed at the same time

Upvotes: 0

Related Questions