Home > Language Reference > Classes > Form
KeyPress Event
Event raised when a character is entered or a key is pressed.
Syntax
Protected Event KeyPress(ByRef iChar As Integer, ByRef iKeyCode As Integer, ByRef eModifiers As HbKeyModifier, ByRef bForward As Boolean)
| Parameter | Description |
| iChar | Character code |
| iKeyCode | Virtual key code (used when handling 5-way navigators) |
| eModifiers | Character modifiers. |
| bForward | Indicates whether the system should continue to process the event or not. |
Remarks
The KeyPress event is raised each time the user draws a character in the writing recognition area, or each time a key is pressed. Even if a control has the focus, the form always receives this event, before the character is sent to the control.
The iChar parameter indicates the character code recognized or the key pressed. If the value is less than 256, the character is an ANSI standard character. Otherwise, it represents a control key or code, for which HB++ has the following built-in constants:
| Constant | Value | Description |
| hbCharLowBattery | &H0101 | Display low battery dialog. |
| hbCharMenu | &H0105 | Open the menu. |
| hbCharCommand | &H0106 | Open the command bar. |
| hbCharLaunch | &H0108 | Return to the launcher. |
| hbCharKeyboard | &H0109 | Popup the keyboard in appropriate mode. |
| hbCharFind | &H010A | Open the global search dialog box. |
| hbCharCalc | &H010B | Switch to the calculator application. |
| hbCharAlarm | &H010D | Sent before displaying an alarm. |
| hbCharRonamatic | &H010E | Stroke from graffiti area to top half of screen. |
| hbCharGraffitiReference | &H010F | Popup the Graffiti reference. |
| hbCharKeyboardAlpha | &H0110 | Popup the keyboard in alpha mode. |
| hbCharKeyboardNumeric | &H0111 | Popup the keyboard in number mode. |
| hbCharLock | &H0112 | Switch to the Security application and lock the device. |
| hbCharBacklight | &H0113 | Backlight on/off. |
| hbCharAutoOff | &H0114 | Sent when the inactivity timer elapses. |
| hbCharPowerOff | &H011E | Switch off the device. |
| hbCharLateWakeup | &H0120 | Sent after the device waking up. |
| hbCharAttnStateChanged | &H0126 | Posted by AttentionMgr API to open or update dialog. |
| hbCharAttnUnsnooze | &H0127 | Posted when AttentionMgr snooze timer expires. |
| hbCharAttnIndicatorTapped | &H0128 | Posted when AttentionIndicator is tapped. |
| hbCharAttnAllowClose | &H0129 | Posted when AttnAllowClose is called. |
| hbCharAttnReopen | &H012A | Posted when AttnReopen is called. |
| hbCharCardCloseMenu | &H012B | Posted when a card is inserted. |
| hbCharIrGotData | &H012C | Posted when IR receive is initiated and copying of an app is imminent. |
| hbCharInputAreaControl | &H0137 | Toggle for opening and closing input area. |
| hbCharBluetoothSetup | &H0138 | Go to Bluetooth panel. |
| hbCharHard1 | &H0204 | Application #1 launch button. |
| hbCharHard2 | &H0205 | Application #2 launch button. |
| hbCharHard3 | &H0206 | Application #3 launch button. |
| hbCharHard4 | &H0207 | Application #4 launch button. |
| hbCharHardPower | &H0208 | On/Off button. |
| hbCharHardCradle | &H0209 | Cradle synchronization button. |
| hbCharHardCradle2 | &H020A | Alternate cradle synchronization button. |
| hbCharHardContrast | &H020B | Contrast adjustment button. |
Numerous other codes are possible, depending on the Palm OS® version and the handheld device model being used.
The iKeyCode parameter is the virtual code for the entered character. It is unused on most platforms; its value only matters when handling 5-way navigators, and other special hard keys. Unfortunately, most devices do not agree on its signification. You should refer to the each licensee's developer documentation for more information.
The eModifiers parameter specifies additional information about the entered character, and can be zero, one, or more of the following:
| Constant | Value | Description |
| hbKeyShift | &H0001 | Graffiti or Graffiti 2 is in case-shift mode. |
| hbKeyCapsLock | &H0002 | Graffiti is in cap-shift mode. (Cap-shift mode is not supported in Graffiti 2.) |
| hbKeyNumLock | &H0004 | Graffiti or Graffiti 2 is in numeric-shift mode. |
| hbKeyCommand | &H0008 | The glyph was the menu command glyph or a virtual key code. |
| hbKeyAutoRepeat | &H0040 | Event was generated due to auto-repeat. |
| hbKeyPoweredOn | &H0100 | The key press caused the system to be powered on. |
The bForward parameter allows handling of the event by the system to be blocked. If you sets its value to False, when this event is being handled, the character or key pressed is ignored. If a control has the focus, it does not receive the character entered. If a key is pressed, it has no effect.
The following example uses this functionality to prevent a user from exiting the current application. All the keys apart from on/off are blocked:
Private Sub Form_KeyPress(ByRef iChar As Integer, ByRef iKeyCode As Integer, ByRef iModifiers As HbKeyModifier, ByRef bForward As Boolean) If iChar>=256 And iChar<>hbCharHardPower Then bForward=False End Sub
Note that the iChar, iKeyCode, and eModifiers parameters are passed by reference. This means that your application can change the value of those parameters on the fly, before the system processes the event.
System requirements
| System | Minimal version | Remarks |
| Palm OS | Palm OS 3.0 | - |
| Windows CE | Windows CE 3.0 | - |