Gambas Documentation
Documents

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.

Warning, Care should be taken as some controls will swallow the keypress event.

This depends both on the control and the toolkit being used.