Interacting with the Keyboard

   

Most every control handles its own keyboard input; however, 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 button or releases a specific button. Most controls support three events that you can use to work directly with keyboard input. These are listed in Table 18.4.

Table 18.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 (the key has been pushed down and then released) while the control has the focus.
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 18.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. When the user releases a key, the KeyPress event fires.

  3. After the KeyPress event fires, 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 display the letter "k." Start by creating a new Windows Application titled Keyboard Example. Change the name of the default form to fclsKeyboardExample, set its Text property to Keyboard Example, and set the Main() entry point of the project to reference fclsKeyboardExample instead of Form1. 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
Text ( make blank )

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 open the events list in the Properties window (Remember, this is the lightning bolt icon). Scroll through the list and locate the KeyPress event. Next, double-click the KeyPress event to access it in code. Your code editor should look now look like Figure 18.8.

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

graphics/18fig08.jpg


As you learned in Hour 4, "Understanding Events," the e parameter contains information about the occurrence of this event. In the keyboard events, the e parameter contains information about the key being pressed; therefore, 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 are going to write code that handles the keystroke when the key being pressed is "k." Add the following code to the KeyPress event:

 if (e.KeyChar == 'k')         e.Handled = true; 
graphics/bookpencil.gif

Be sure to surround the k with single quotes, not double-quotes, because you're dealing with a character (char), not a string.

I would imagine that you're curious about the Handled property of the e object. When you set this property to true, you are telling C# that you handled the keystroke, and C# should ignore it. To see the effect this has, press F5 to run the project and enter the following into the text box:

Heard any good jokes lately?

What you'll end up with is the text shown in Figure 18.9.

Figure 18.9. Notice how the letter k was "eaten" by your code.

graphics/18fig09.jpg


Go ahead, try to enter another "k" ”you can't. Next, try to enter an uppercase "K"; C# allows you to do this because uppercase and lowercase characters are considered different characters. Want to catch all "Ks," 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; 
graphics/bookpencil.gif

It's not often that I need to catch a keypress, but every now and then I do. The three keystroke events have always made it easy to do what I need to do, but if there's any 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.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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