Export (0) Print
Expand All

Shape.KeyPress Event

Occurs when a key is pressed and the shape has focus.

Namespace:  Microsoft.VisualBasic.PowerPacks
Assembly:  Microsoft.VisualBasic.PowerPacks.Vs (in Microsoft.VisualBasic.PowerPacks.Vs.dll)

[BrowsableAttribute(true)]
public event KeyPressEventHandler KeyPress

Key events occur in the following order:

KeyDown

KeyPress

KeyUp

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

Use the KeyChar property to sample keystrokes at run time and to consume or modify a subset of common keystrokes.

To handle keyboard events only at the form level and not enable shapes to receive keyboard events, set the Handled property in the form's KeyPress event-handling method to true.

For more information about how to handle events, see Consuming Events.

The following example shows how to respond to the KeyPress event in an event handler. This example requires that you have a RectangleShape control named RectangleShape1 on a form.

private void rectangleShape1_KeyPress(object sender, 
    System.Windows.Forms.KeyPressEventArgs e)
{
    char ch;
    ch = e.KeyChar;
    MessageBox.Show("You pressed the " + ch + " key.");
}

Show:
© 2014 Microsoft