Keypress on a Form

Post

Posted
Rating:
#1 (In Topic #507)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 Hi All,

I am new to Gambas and I have been trying to get a example program to print a message to a label when I press F1 but I can not get it to work

This is the code behind the form


Public Sub Form_Open
'
End

Public Sub Form_KeyPress
 If Key.F1 Then LCDLabel2.text = Time & " F1 Key Pressed"
End

I am use to using VB6 and VB.net but in them languages I had to set the Form keypreview to true but i can not see that option in Gambas.

Can someone please help me as I have big plans for this app.

Also can I ask iof Gambas supports MDI application development?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
cage is in the usergroup ‘Regular’
Here is examples of how I do it…

Code (gambas)

  1. Take note of when a key is released
  2. Public Sub Form_KeyRelease()
  3.  
  4.   If (Key.Code = Key.ShiftKey) Then
  5.     ShiftPressed = False
  6.     Speed = 1
  7.   End If
  8.   If Key.Code = Key.Up Then UpPressed = False
  9.   If Key.Code = Key.Down Then DownPressed = False
  10.   If Key.Code = Key.Left Then LeftPressed = False
  11.   If Key.Code = Key.Right Then RightPressed = False
  12.   UpdateLabels()
  13.  
  14.  
  15. ' another one
  16. Public Sub Form_KeyPress()
  17.   Print Key.Code, Key.Text
  18.  
  19. Enter and Return.
  20.  
  21. Public Sub Form_KeyPress()
  22.  
  23. 'Main keyboard returns Return and Keypad is Enter
  24.  
  25. TextBox1.Text = "Enter"
  26.  
  27.  
  28.    
  29.  
  30. The Key Code Program
  31. With this simple miniprogram you can check out the keycode Of a button you have pressed.You only need a Form To get the program going.Once you started the program And you Use a button On your keyboard, the keycode Is Printed In the terminal window.
  32. The Code
  33. Public Sub Form_KeyRelease()
  34. Print key.code
  35. The Key Release Program
  36. With this miniprogram you can check the arrow keys.When they are used And released a New Information Is Shown.
  37. You need a textbox To get the program going.
  38. Once you started the program And you Use an arrow key In the textbox, the content Of the textbox will change.
  39. The Code
  40. Public Sub TextBox1_KeyRelease()
  41. Select Key.code
  42.   Textbox1.Text = "Left"
  43.   Textbox1.Text = "Right"
  44.  Case Key.up
  45.   Textbox1.Text = "Up"
  46.  Case Key.down
  47.   Textbox1.Text = "Down"
  48.  
  49. Input Restrictions
  50.  
  51. If you want a textbox To accept only digits you should Use the command Stop EVENT.
  52. Example
  53. You need a textbox On your form To get it going.
  54. Public Sub MyTextBox_KeyPress()
  55.   If InStr("0123456789", Key.Text) = 0 Then
  56.    Stop Event
  57. Example2
  58. You can reach nearly the same With the following code:
  59. Public Sub TextBox1_KeyPress()
  60.    If key.Code >= 48 And key.Code <= 57 Then
  61.    Else If key.Code = key.BackSpace Then
  62.    Else If key.Code = key.Delete Then
  63.    Else
  64.      Stop Event
  65. Public Sub Form_Open()
  66.  Me.Text = "Only digits accepted !"

Hope that helps.  Yes Gambas does do MDI just not quite like VB did.  Take a look at my Handyman program in the showcase section of the site.  It uses Gambas MDI.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
Hi AndyGable and welcome to the forum

Here is my input to your question.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   ''All the Properies here can be made in the IDE
  4.  
  5.   With Me
  6.     .Height = 150
  7.     .Width = 900
  8.     .Padding = 5
  9.     .Arrangement = Arrange.Fill
  10.  
  11.   With LCDLabel2
  12.     .Alignment = Align.Center
  13.     .Expand = True
  14.     .Padding = 20
  15.     .Sheared = True
  16.     .Background = Color.Black
  17.     .Foreground = Color.Black
  18.     .HighlightColor = Color.Green
  19.  
  20.  
  21. Public Sub Form_KeyPress()
  22.  
  23.   'If Key.F1 Then LCDLabel2.text = Time & " F1 Key Pressed"
  24.   If Key.Code = Key.F1 Then LCDLabel2.text = Str(Time(Now)) & " F1 Key Pressed"
  25.  

<IMG src="https://www.cogier.com/gambas/Time%20-%20F1.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Hi , welcome :)

An alternative way would be if you have set up a menu item (use the menu editor) that does the job and simply Set the F1 shortcut key on it.
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 Hi All,

THank you for the replay I have to change it to a GTK+ application and now it works

Is there such a thing as a MDI application in gambas? (i can do do a MDI in VB.net) I ask as I want to transfer my Windows application
into Linux and I use multiple forms to create the look I want for the app
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
Technopeasant is in the usergroup ‘Regular’
You can embed forms into other forms in Gambas, if that is what you are looking for.
Online now: No Back to the top
1 guest and 0 members have just viewed this.