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)

ParameterDescription
iCharCharacter code
iKeyCodeVirtual key code (used when handling 5-way navigators)
eModifiersCharacter modifiers.
bForwardIndicates 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:

ConstantValueDescription
hbCharLowBattery&H0101Display low battery dialog.
hbCharMenu&H0105Open the menu.
hbCharCommand&H0106Open the command bar.
hbCharLaunch&H0108Return to the launcher.
hbCharKeyboard&H0109Popup the keyboard in appropriate mode.
hbCharFind&H010AOpen the global search dialog box.
hbCharCalc&H010BSwitch to the calculator application.
hbCharAlarm&H010DSent before displaying an alarm.
hbCharRonamatic&H010EStroke from graffiti area to top half of screen.
hbCharGraffitiReference&H010FPopup the Graffiti reference.
hbCharKeyboardAlpha&H0110Popup the keyboard in alpha mode.
hbCharKeyboardNumeric&H0111Popup the keyboard in number mode.
hbCharLock&H0112Switch to the Security application and lock the device.
hbCharBacklight&H0113Backlight on/off.
hbCharAutoOff&H0114Sent when the inactivity timer elapses.
hbCharPowerOff&H011ESwitch off the device.
hbCharLateWakeup&H0120Sent after the device waking up.
hbCharAttnStateChanged&H0126Posted by AttentionMgr API to open or update dialog.
hbCharAttnUnsnooze&H0127Posted when AttentionMgr snooze timer expires.
hbCharAttnIndicatorTapped&H0128Posted when AttentionIndicator is tapped.
hbCharAttnAllowClose&H0129Posted when AttnAllowClose is called.
hbCharAttnReopen&H012APosted when AttnReopen is called.
hbCharCardCloseMenu&H012BPosted when a card is inserted.
hbCharIrGotData&H012CPosted when IR receive is initiated and copying of an app is imminent.
hbCharInputAreaControl&H0137Toggle for opening and closing input area.
hbCharBluetoothSetup&H0138Go to Bluetooth panel.
hbCharHard1&H0204Application #1 launch button.
hbCharHard2&H0205Application #2 launch button.
hbCharHard3&H0206Application #3 launch button.
hbCharHard4&H0207Application #4 launch button.
hbCharHardPower&H0208On/Off button.
hbCharHardCradle&H0209Cradle synchronization button.
hbCharHardCradle2&H020AAlternate cradle synchronization button.
hbCharHardContrast&H020BContrast 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:

ConstantValueDescription
hbKeyShift&H0001Graffiti or Graffiti 2 is in case-shift mode.
hbKeyCapsLock&H0002Graffiti is in cap-shift mode. (Cap-shift mode is not supported in Graffiti 2.)
hbKeyNumLock&H0004Graffiti or Graffiti 2 is in numeric-shift mode.
hbKeyCommand&H0008The glyph was the menu command glyph or a virtual key code.
hbKeyAutoRepeat&H0040Event was generated due to auto-repeat.
hbKeyPoweredOn&H0100The 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

SystemMinimal versionRemarks
Palm OSPalm OS 3.0-
Windows CEWindows CE 3.0-