Methods | Fields | This Package | All Packages

KeyPressEvent Class

Provides data for the keyPress event of a Control object.

Event
  |
  +--KeyPressEvent

package com.ms.wfc.ui

public class KeyPressEvent
extends
Event

Remarks

When the user presses a key on the keyboard, three events can be generated: keyDown, keyUp, and keyPress. 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 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.

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

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 Handling Events in WFC.