Input Fields

Blitz3D Forums/Blitz3D Beginners Area/Input Fields

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

First of all, you need a 'status' variable, and some status constants to be used for:
Global Status
Global Input_Text$ ;store the text input of the user
Global Input_Finished = False ;flag to indicate when the user is done with the input
Const S_NORMAL = 1
Const S_INPUT  = 2
Status = S_NORMAL


When the user presses the 'input text button', the status variable, which normally is set to S_NORMAL, will be set to status S_INPUT.

You need this status variable to decide, in your code, when your program should display a rectangle on the screen, simulating an imput text box, and when not:

while not keydown(1) ;while not esc is pressed

Cls

;code logic here

if keyhit(your_input_key) then ;make input box available
status = S_INPUT ;set the status variable
Input_Text$ = "" ;reset the input text
Input_Finished = False ;reset the input finished flag
endif

if Input_Finished then ;user pressed enter, so input has finished
status = S_NORMAL
endif

select Status
case S_NORMAL
    ;display everything but the input box
    ;check the keyboard as usually
case S_INPUT
    ;display everything + the input box
    ;check the keyboard for input box
    Input_Box() ;the function to read user input
    ;display an input box rectangle
    Rect 100,100,150,80
    ;display the text inside it
    Text 110,110,Input_Text$

end select
flip
wend


Let's now talk about the function Input_Box(). With this function, your program should get the user input, and update the variable string Input_Text$. You may use the GetKey command, which returns the ASCII value of a character, so that your program does not stop while executing the function Input_Box.

A short example would resemble like this:
Function Input_Box()
local av = GetKey() ;gets the ascii value of a pressed key
if av > 0 then ;a key has been pressed
;first check if the user has pressed enter
If av = enter_ascii_code then
Input_Finished = True
Return ;exit the function
endif
;we have to convert the ascii value, which is an integer, to the corresponding string character using the command Chr, and update the input text:

Input_Text = Input_Text + Chr(av)

endif
end function


Note that the function Input_Text() is not complete yet. In fact, you need to find out the ascii code of the enter key, and also handle the delete key. But I think - and hope - that the code provided will help you to find out the way, otherwise post again if you are in trouble :)

You may also find the code archive a mine gold; look in the topic 'User Input', you'll find functions that already make what you need:
http://www.blitzbasic.com/codearcs/codearcs.php?cat=5


Sergio.