Extracting information about key pressed in .NET console applications

Console applications let you extract the key(s) pressed by the user using the Console.ReadKey() method. It returns an object of type ConsoleKeyInfo which includes a number of useful properties.

ConsoleKeyInfo has the following properties:

  • Key: returns an enumeration of type ConsoleKey. Examples include ConsoleKey.A, ConsoleKey.PrintScreen, ConsoleKey.Help etc., i.e. there’s an entry for each key on a keyboard. There are more than 240 values in this enumeration.
  • KeyChar: same as the above but it returns the key pressed as a char where it makes sense. E.g. F11 is not associated with a character, KeyChar will be an empty character ‘\0’. Also, if the combined value of the keys pressed produces whitespace or an empty character then KeyChar will be ‘\0’. Example: ctrl+alt+s
  • Modifiers: returns an enumeration of type ConsoleModifiers which holds the values for the Alt, Shift and Control keys

The following code sample prints each of these properties:

static void Main(string[] args)
{
	RunPressKeyCodeSample();
}

private static void RunPressKeyCodeSample()
{
	ConsoleKeyInfo keyInfo = Console.ReadKey();
	ConsoleKey keyPressed = keyInfo.Key;
	Debug.WriteLine("Key pressed: {0}", keyPressed);
	char keyChar = keyInfo.KeyChar;
	Debug.WriteLine("Char format: {0}", keyChar);
	ConsoleModifiers modifiers = keyInfo.Modifiers;
	Debug.WriteLine("Any modifiers: {0}", modifiers);
}

Here’s an example output when ctrl+shift+F11 are pressed:

Key pressed: F11
Char format:
Any modifiers: Shift, Control

Here comes another example when shift+p are pressed:

Key pressed: P
Char format: P
Any modifiers: Shift

View all various C# language feature related posts here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Extracting information about key pressed in .NET console applications

  1. Vikram says:

    Thanks a lot for the nice post. Very useful information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: