Browse DevX
Sign up for e-mail newsletters from DevX


Exploring Secrets of .NET Keystroke Handling

If you've ever been frustrated trying to figure out how to intercept or assign specific keystrokes to specific controls, you'll be glad you found this article.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

ew areas in .NET are seemingly as simple yet deceptively challenging as processing keyboard inputs. This situation is exacerbated because neither the MSDN documentation nor any of the excellent .NET support websites provide comprehensive, practical details about handling keystrokes. Enter Keystroke Sandbox, a small application developed just for this article. Keystroke Sandbox shows you graphically what happens when you press a single key or a combination of keys. Furthermore, it lets you customize its environment at runtime to emulate a variety of the most common Windows Forms application patterns, including both enabling/disabling controls to receive or ignore input as well as simulating consuming keystrokes at different stages and using different controls.

This article covers the different types of keyboard interaction that an application may have. You'll see how you can simplify debugging by observing which methods get invoked on which controls when you press a key. The article concludes with a practical guide for implementing a variety of common key-handling scenarios.

What You Need
  • Visual Studio 2008
  • C# 3.0
  • .NET Framework: 2.0 or higher

Keystroke/ Program Interactions
Before considering the practical matter of wiring up key handlers in your code, Table 1 shows a handy reference guide to all the typical situations where your programs need to handle keyboard input (although there are certainly other, more abstruse situations where keyboard input would be required).

Table 1. Types of Keyboard Interactions: The table lists all the common types of keyboard/program interaction.
Element Character class Example or Details
Open a fixed menu
(i.e. on a menu bar)
Alt + alphanumeric Alt+F typically opens the file menu.
Mnemonic on a fixed menu item alphanumeric The File menu frequently contains an Exit choice, with the underlined key identifying a mnemonic key that activates that choice.
Mnemonic on a context menu item alphanumeric A mnemonic key defined on any menu item on a control's context menu. Users open the context menu either by right-clicking or by pressing the Menu key on most modern keyboards.
Mnemonic on another control Alt + alphanumeric By defining a mnemonic key on a control such as a Button, users can click the button using Alt plus that mnemonic (or just the mnemonic depending on where the focus is)—just as if they had clicked it with the mouse.
Shortcut on a fixed menu item (modifiers) alphanumeric Shortcut keys are combinations that call a menu item's underlying handler just as if a user had clicked that item. For example, in most applications the Edit menu defines Control+C as a shortcut key for the Copy menu item, Control+V as a shortcut for Paste, and Control+X for Cut.
Modify a mouse click (modifiers) Use this when you want to distinguish between an unadorned mouse click and a Control click (depressing Control while clicking with the mouse).
Echo a typed character (Shift) alphanumeric An example would be to display <SPAN CLASS="PF">A</SPAN> when a user types Shift+A.