comp • gb.qt4 • control • .keypress

Event Control.KeyPress (gb.qt4)

Event KeyPress ( )

Raised when a key is pressed while the control has the focus.

You get information about the pressed key with the Key class.

After the KeyPress routine has finished, the Text inside a TextBox is edited by Gambas, according to the key. So it makes no sense to write back into the Text property any information from the KeyPress event handler, nor from any of the subroutines and functions it calls. Rather use the Change event, which comes after updating the content.

Example

Public Sub n_Keypress()
' Event handler for the Group n - this is the 9 by 9 array of TextBoxes
Dim ltext As Variant
Dim ltag As Variant


ltext = Key.Text ' Get the key text

ltag = Last.Tag ' The Group Property
If Not IsNull(ltext) Then
  If Mid(ltext, 1) >= "0" And Mid(ltext, 1) <= "9" Then
    ' Has pressed one of the numeric keys 0 to 9
    z[ltag] = Int(Asc(ltext) - 48) ' convert string to integer and save in integer array
...

' must not update the field as e.g.: n.Text = 48 + i

Event Cancellation

If you stop the event with STOP EVENT, the control won't see the keyboard event at all.