Keyboard events are events that refer to events of visible control elements (components) associated with the keyboard.
You will get information about the pressed key if you use the Class Key key described in chapter 14.1.1.1.
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 8.3.3.3 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 14.1.3.2.1: Entering a color value
Figure 14.1.3.2.2: 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.Center FMain.Resizable = False txbColourvalue.Clear 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 Else 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)!") txbColourvalue.SetFocus Return Endif ' txbColourvalue.Text = "" ? If Match(sSubject, sPattern) = True Then PictureBox1.Hide pPanel.Show If Len(txbColourvalue.Text) = 6 Then pPanel.Background = ZModul.NumberToDezimal(Upper(txbColourvalue.Text), 16) Else pPanel.Background = ZModul.NumberToDezimal(Right(Upper(txbColourvalue.Text), 6), 16) Endif ' Len(txbColourvalue.Text) = 6 ? Wait 2 ' Colour is displayed for 2 seconds txbColourvalue.Clear txbColourvalue.SetFocus PictureBox1.Show Else Message.Info("The HTML colour value is not correct.") txbColourvalue.SetFocus Endif ' Match(sSubject, sPattern) = True ? pPanel.Delete 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 Else 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>" Message.Info(sMessage) txbColourvalue.SetFocus End ' btnHelp_Click()
Hints:
Project