Syntaxus Dogmata

An Insane Developer's Journal

“I am the KeyMaster (App)”

While putting together Ken’s bingo game example this past weekend, it occurred to me that with all the game development studies I’ve been pursuing lately, I haven’t really grasped console input in C#.  After digging into the Console class, and its corresponding ConsoleKey enumeration, the further thought occurred that the nomenclatures they assign to many keystrokes make about as much sense as Muhammad Ali in a post-match interview on ABC’s Wide World of Sports. (Thank you, Microsoft!)

Masochist that I am, I decided to address the matter once and for all, so I thew together a little thing I call the KeyMaster.  Nothing too complex, really.  All it does is read each keystroke from the keyboard, and reveal any relevant data aggregated among C#’s ConsoleKeyInfo struct members.  You’ll find the complete source at the bottom of this post.

KeyMaster also offered me the opportunity to get some delegate practice in.  Ctrl-C and Ctrl-Break are handled differently, both of which typically close console applications.  KeyMaster intercepts a Ctrl-C just fine, but C# throws a tizzy fit if you dare attempt to intercept the Ctrl-Break the same way.  C’est la vie.

Anyway, here’s the complete source.  Hopefully someone will find it useful…


using System;

namespace KeyMaster {

	public class KeyMaster {

		protected ConsoleKeyInfo myKeyInfo;

		// delegate for handling special keystrokes (ctrl-c & ctrl-break)
		public void ConsoleCancelEventHandler(object sender, ConsoleCancelEventArgs args) {
			ConsoleSpecialKey key = args.SpecialKey;

			// check for ctrl-break
			if (key == ConsoleSpecialKey.ControlBreak) {
				Console.CancelKeyPress -= ConsoleCancelEventHandler;                    // remove the special keystroke handler delegate
				return;
			}

			// ctrl-c was pressed
			myKeyInfo = new ConsoleKeyInfo((char)3, ConsoleKey.C, false, false, true);      // handle ctrl-c like other strokes
			RefreshDisplay();                                                               // show it on screen
			args.Cancel = true;                                                             // cancel closing the app
		}

		public void RefreshDisplay() {
			Console.Clear();
			Console.Write("\nPress a key...\n(Ctrl+Break to Quit)\n\n");
			Console.Write("\t   Character: {0}\n", myKeyInfo.KeyChar);
			Console.Write("\tNomenclature: {0}\n", myKeyInfo.Key);
			Console.Write("\t   Modifiers: {0}\n", myKeyInfo.Modifiers);
		}

		public void Run() {
			Console.CancelKeyPress += ConsoleCancelEventHandler;                            // add the special keystroke handler delegate

			do {
				RefreshDisplay();                                                       // show the latest keystroke info on screen
				myKeyInfo = Console.ReadKey(true);                                      // wait for another keystroke
			} while (true);
		}

	}

	class Program {

		static void Main(string[] args) {
			KeyMaster km = new KeyMaster();
			km.Run();
		}

	}

}

Advertisements

Mar 22, 2010 - Posted by | Tools | , , , ,

No comments yet.

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

%d bloggers like this: