User Tools

Site Tools


14.1.3 Keyboard and events

Keyboard events are events that refer to events of visible control elements (components) associated with the keyboard. Overview of keyboard events

  • Control.KeyPress (gb.qt4)
    The event (event) is triggered when a button is pressed while the control has the focus.
  • Control.KeyRelease (gb.qt4)
    The event (event) is triggered when a button is released while the control has the focus.

You will get information about the pressed key if you use the Class Key key described in chapter Project

The following project tries to evaluate the information about a pressed key and to react appropriately to it in the program. In the text field, only characters from a highly restricted character range [&, 0-9, a-f, A-F] are used. (→ Chapter LIKE operator), whereby the &-character may only be entered as the first character (optional). The correct syntax of the entered color value is checked with a regular expression. If the color value is syntactically correct, the corresponding color is displayed briefly, otherwise an error message is displayed.

Figure Entering a color value

Figure Display of the color to the color value

The completely specified source code must also be considered under this aspect. In a productive project, coding would be more effective because some restrictions are doubly secured.

' Gambas class file
Private sSubject As String
Private sPattern As String
Public Sub Form_Open()
  FMain.Text = "KeyPress - KeyRelease"
  FMain.Resizable = False
  txbColourvalue.MaxLength = 6
  PictureBox1.Stretch = True
  PictureBox1.Picture = Picture["Symbols/colour.png"]
End ' Form_Open()
Public Sub txbColourvalue_KeyPress()
  If Key.Control And Key.Code = Key.F1 Then btnHelp_Click() ' Help function with CTRL+F1
  If (Key.Code = Key.Return Or Key.Code = Key.Enter) And txbColourvalue.Text Then
     Message.Info("The entered Characters are:\n\n" & Upper(txbColourvalue.Text))
     If Left(txbColourvalue.Text, 1) = "&" Then
        txbColourvalue.MaxLength = 7
        txbColourvalue.MaxLength = 6
     Endif ' Left(txbColourvalue.Text, 1) = "&" ?
  Endif ' Key.Code = Key.Return Or Key.Code = Key.Enter ?
  If Key.Code = Key.BackSpace And Len(txbColourvalue.Text) > 0 Then
     txbColourvalue.Text = Left(txbColourvalue.Text, Len(txbColourvalue.Text) - 1)
  Endif ' Key.Code = Key.BackSpace And Len(txbColourvalue.Text) > 0 ?
' Permissible characters for a colour value in hexadecimal representation
  If Key.Text Not Like "[&0-9a-fA-F]" Then
     Stop Event
  Endif ' Key.Text Not Like "[&0-9a-fA-F]"
End ' txbColourvalue_KeyPress()
Public Sub txbColourvalue_KeyRelease()
  If Len(txbColourvalue.Text) = 0 Then txbColourvalue.MaxLength = 6
  If Left(txbColourvalue.Text, 1) = "&" Then txbColourvalue.MaxLength = 7
End ' txbColourvalue_KeyRelease()
Public Sub btnCheckColorValue_Click()
  Dim pPanel As Panel
  pPanel = New Panel(FMain)
  pPanel.H = 88
  pPanel.W = pPanel.H
  pPanel.x = 208
  pPanel.y = 16
  pPanel.Border = Border.Raised
  sSubject = txbColourvalue.Text
  sPattern = "^(&)?[a-fA-F0-9]{6}$"
  If Not txbColourvalue.Text Then
     Message.Warning("Enter a colour value (hex)!")
  Endif ' txbColourvalue.Text = "" ?
  If Match(sSubject, sPattern) = True Then
     If Len(txbColourvalue.Text) = 6 Then
        pPanel.Background = ZModul.NumberToDezimal(Upper(txbColourvalue.Text), 16)
        pPanel.Background = ZModul.NumberToDezimal(Right(Upper(txbColourvalue.Text), 6), 16)
     Endif ' Len(txbColourvalue.Text) = 6 ?
     Wait 2 ' Colour is displayed for 2 seconds
     Message.Info("The HTML colour value is not correct.")
  Endif ' Match(sSubject, sPattern) = True ?
End ' btnCheckColorValue_Click()
'*** ADDITIONS *******************************************************************************
Public Function Match(Subject As String, Pattern As String) As Boolean
  Dim rRegex As Regexp
  rRegex = New Regexp(Subject, Pattern)
  If rRegex.Offset = -1 Then
     Return False
     Return True
  Endif ' rRegex.Offset = -1
End ' Match(Subject As String, Pattern As String) As Boolean
Public Sub btnHelp_Click()
  Dim sMessage As String
  sMessage = "<hr><b>Help on the character stock</b><hr>"
  sMessage &= "-> Character &"
  sMessage &= "<br>-> Digits 0-9"
  sMessage &= "<br>-> Letters from the fields a-f and A-F<hr>"
End ' btnHelp_Click()


  • Please note that the keyboard events (KeyPress and KeyRelease) only refer to one component - here the text field txbColourValue.
  • The small program help is called up in the project using the small blue button on the one hand and with the unusual key combination CTRL+F1 on the other hand, in order to also edit the case for a key combination.
  • The module for converting numbers (basis 2.. 32) into numbers of base 10 (decimal numbers) is located in the repository.




The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k14/k14.1/k14.1.3/start.txt · Last modified: 11.02.2022 (external edit)

Page Tools