Keypress on a Form

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 362
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Keypress on a Form

Post by AndyGable »

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?
cage
Posts: 123
Joined: Monday 2nd September 2019 5:47am
Location: Phoenix Arizona

Re: Keypress on a Form

Post by cage »

Here is examples of how I do it...
Take note of when a key is released
Public Sub Form_KeyRelease()

  If (Key.Code = Key.ShiftKey) Then
    ShiftPressed = False
    Speed = 1
  End If
  If Key.Code = Key.Up Then UpPressed = False
  If Key.Code = Key.Down Then DownPressed = False
  If Key.Code = Key.Left Then LeftPressed = False
  If Key.Code = Key.Right Then RightPressed = False
  UpdateLabels()

End
 
' another one
Public Sub Form_KeyPress()
  Print Key.Code, Key.Text
End 

Enter and Return.

Public Sub Form_KeyPress()

'Main keyboard returns Return and Keypad is Enter
If Key.Code = Key.Return Then

TextBox1.Text = "Enter"


Endif
   
End

The Key Code Program
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.
The Code
Public Sub Form_KeyRelease()
Print key.code
End 
The Key Release Program
With this miniprogram you can check the arrow keys.When they are used And released a New Information Is Shown.
You need a textbox To get the program going.
Once you started the program And you Use an arrow key In the textbox, the content Of the textbox will change.
The Code
Public Sub TextBox1_KeyRelease()
Select Key.code
 Case Key.left
  Textbox1.Text = "Left"
 Case Key.right
  Textbox1.Text = "Right"
 Case Key.up
  Textbox1.Text = "Up"
 Case Key.down
  Textbox1.Text = "Down"
End Select
End

Input Restrictions

If you want a textbox To accept only digits you should Use the command Stop EVENT.
Example
You need a textbox On your form To get it going.
Public Sub MyTextBox_KeyPress()
  If InStr("0123456789", Key.Text) = 0 Then
   Stop Event
 Endif
End Sub
Example2
You can reach nearly the same With the following code:
Public Sub TextBox1_KeyPress()
   If key.Code >= 48 And key.Code <= 57 Then 
   Else If key.Code = key.BackSpace Then 
   Else If key.Code = key.Delete Then 
   Else 
     Stop Event 
 Endif 
End
Public Sub Form_Open()
 Me.Text = "Only digits accepted !"
End
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.
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Keypress on a Form

Post by cogier »

Hi AndyGable and welcome to the forum

Here is my input to your question.
Public Sub Form_Open()

  ''All the Properies here can be made in the IDE

  With Me
    .Height = 150
    .Width = 900
    .Padding = 5
    .Arrangement = Arrange.Fill
  End With

  With LCDLabel2
    .Alignment = Align.Center
    .Expand = True
    .Padding = 20
    .Sheared = True
    .Background = Color.Black
    .Foreground = Color.Black
    .HighlightColor = Color.Green
  End With

End

Public Sub Form_KeyPress()

  'If Key.F1 Then LCDLabel2.text = Time & " F1 Key Pressed"
  If Key.Code = Key.F1 Then LCDLabel2.text = Str(Time(Now)) & " F1 Key Pressed"

End
Image
User avatar
BruceSteers
Posts: 1563
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Keypress on a Form

Post by BruceSteers »

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.
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 362
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Keypress on a Form

Post by AndyGable »

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
User avatar
Technopeasant
Posts: 144
Joined: Saturday 13th July 2019 6:50pm
Location: Stony Plain, Alberta, Canada
Contact:

Re: Keypress on a Form

Post by Technopeasant »

You can embed forms into other forms in Gambas, if that is what you are looking for.
Technical director,
Piga Software
http://icculus.org/piga/
Post Reply