Interacting with the Keyboard


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.

Table 17.4. Events That Handle Keyboard Input

Event Name

Description

KeyDown

Occurs when a key is pressed down while the control has the focus.

KeyPress

Occurs when a key is pressed while the control has the focus. If the user holds the key down, this event will fire multiple times.

KeyUp

Occurs when a key is released while the control has the focus.


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:

1.

When the user presses a key, the KeyDown event fires.

2.

Once the key is down, the KeyPress event fires. This event will repeat as long as the key is held down.

3.

When the user releases a key, the KeyUp event fires, completing the cycle of keystroke events.

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:

1.

Right-click Form1.cs in the Solution Explorer, choose Rename, and change the name of the default form to frmKeyboardExample.cs. Next, set the form's Text property to Keyboard Example.

2.

Add a new text box to the form and set its properties as shown in the following table:

Property

Value

Name

txtInput

Location

24,80

Multiline

True

Size

240,120


3.

You're going to add code to the KeyPress event of the text box to "eat" keystrokes made with the letter "k." Select the text box on the form and click the Events button on the Properties window to view the text box's events.

4.

Locate and double-click KeyPress in the event list to create a new KeyPress event procedure. Your code editor should look now look like Figure 17.8.

Figure 17.8. The KeyPress event is a good place to handle keyboard entry.


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.





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net