Gambas/Text

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Back to Gambas

The Key Code Program[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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