KeyEvent Class

Methods | Fields | This Package | All Packages

Provides data for the keyDown or keyUp event of a Control object.

Event
  |
  +--KeyEvent

package com.ms.wfc.ui

public class KeyEvent
extends
Event

Remarks

When the user presses a key on the keyboard, three events can be generated: The keyDown, keyUp, and keyPress events. The keyDown event occurs when the user presses a key while this control has the focus. The keyUp event occurs when the user releases a key while this control has the focus. The keyPress event occurs when the user presses a key while this control has the focus. Duplicate keyDown and keyPress events occur each time the key repeats; only one keyUp event is generated when the user releases the key.

A KeyEvent object, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each keyDown and keyUp event. A KeyPressEvent, which specifies the character the user composed, is passed with each keyPress event.

Use the KeyEvent object when you want information about the keys the user pressed. Use the KeyPressEvent object when you want information about the character that was composed by the keys the user pressed.

You can use the logical OR (|) operator to compare data in the KeyEvent object with the constants in the Key class to extract information about modifier keys the user pressed.

For example, the following code checks if the user pressed the SHIFT+K keys:

private void Form1_keyDown(Object source, KeyEvent e)
{
   if(e.keyData == (Key.SHIFT | Key.K))
      System.out.println("The KeyEvent object knows you pressed SHIFT+K.");
}

Another way to approach this would be to check the character the user composed, rather than checking the keys the user pressed. The following example checks if the user composed an uppercase K character:

private void Form1_keyPress(Object source, KeyPressEvent e)
{
   if(e.keyChar == 'K')
      System.out.println("The KeyPressEvent object knows you pressed keys that composed an uppercase K.");
}

For information about the WFC event model, see .