Although most every control on a form handles its own keyboard input, on occasion you'll want to handle keyboard input directly. For example, you might want to perform an action when the user presses a specific key or releases a specific key. Most controls support three events that you can use to work directly with keyboard input. These are listed in Table 17.4.
These events fire in the same order in which they appear in Table 17.4. Suppose, for example, that the user presses a key while a text box has the focus. The following list shows how the events would fire for the text box:
You're now going to create a project that illustrates handling keystrokes. This project has a text box that will refuse to accept the letter "k." Start by creating a new Windows Application titled Keyboard Example and then follow these steps to build the project:
As you learned in Hour 4, "Understanding Events," the e parameter contains information specific to the occurrence of this event. In the keyboard-related events, the e parameter contains information about the key being pressed; it's what you'll be using to work with the keystroke made by the user. The key being pressed is available as the KeyChar property of the e parameter. You're going to write code that handles the keystroke when the pressed key is the letter "k." Add the following code to the KeyPress event: if (e.KeyChar == 'k') e.Handled = true; By the Way Be sure to surround the 'k' with single quotes, not double quotes, because you're dealing with a single character (char), not a string. I imagine that you're curious about the Handled property of the e object. When you set this property to True, you're telling Visual C# that you handled the keystroke, and that Visual C# should ignore it (that is, not add it to the text box). To see the effect this has, press F5 to run the project and enter the following text into the text box: Heard any good jokes lately? What you'll end up with is the text shown in Figure 17.9. Notice how the letter "k" was "eaten" by your code. Figure 17.9. The keyboard events enable you to handle keystrokes as you see fit.
Go ahead, try to enter another "k"you can't. Next, try to enter an uppercase "K"; Visual C# allows you to do this because uppercase and lowercase characters are considered different characters. Want to catch all K's regardless of case? You could do so by adding the OR (||) operand to your decision construct, like this: if (e.KeyChar == 'k'|| e.KeyChar == 'K') e.Handled = true; By the Way When you paste data from the clipboard, the KeyPress event isn't fired for each keystroke. It's therefore possible that a "k" could appear in the text box. If you absolutely needed to keep the letter "k" out of the text box, you'd need to make use of the TextChanged event. Did you Know? It's not often that I need to catch a keypress, but every now and then I do. The three keystroke events listed in Table 17.4 have always made it easy to do what I need to do, but if there's one caveat I've discovered, it's that you need to give careful consideration to which event you choose (such as KeyPress or KeyUp, for example). Different events work best in different situations, and the best thing to do is to start with what seems like the most logical event, test the code, and change the event if necessary. |