input "fields"

Blitz3D Forums/Blitz3D Beginners Area/input "fields"

Yahfree(Posted 2007) [#1]
Hmm, what are other methods of retrieving input and storing it besides "Input()"?

like a simple input box like i'm typing in now for example? any ways to retrive input in a more eye catching way and not like simple type/enter?

how do you create these "input fields" ?


skidracer(Posted 2007) [#2]
Check out the CodeArchives - Input section.

Start at the bottom and you will find several entries to do with such things.


Yahfree(Posted 2007) [#3]
interesting... so to make this possible, you would have to check when a key is pressed, find out what key was pressed and type out on the screen a letter depending on what key is pressed?

whats the easyest way to pull this off? (somthing i can some-what understand)


Matty(Posted 2007) [#4]
In particular I've always found this code archive entry a great help:

http://blitzbasic.com/codearcs/codearcs.php?code=207

Yes you would have to either poll or the keys with keydown() for each key scancode OR use getkeys() if you are only expecting standard keys to be pressed - like letters numbers etc.

What it would usually involve is having a variable, a string variable, which you add characters to each time a key is pressed - as long as you are wanting to detect key presses for that purpose - and then draw/text/write the string variable to the screen. You can even manipulate the string so that it wraps and crosses multiple lines by writing your code so that it parses the string variable appropriately.


Lordon(Posted 2007) [#5]
I'm still learning myself, here's what I did for the login screen on my game:

Hope it helps :]

SetBuffer BackBuffer()
curRow = 1
done = False

While Not done

Color 255,255,255
If curRow = 1 Color 200,200,0
Rect 90,5,100,20,0
Text 95,10,myAccount$

Color 255,255,255
Text 10,10,"Account:"

If curRow = 2 Color 200,200,0
Rect 90,45,100,20,0

hidden$ = ""

For i = 1 To Len(myPassword$)
hidden$ = hidden$+"*"
Next

Text 95,50,hidden$
Color 255,255,255
Text 10,50,"Password:"

If curRow = 3 Color 200,200,0
Text 10,90,"Submit"

If KeyHit(200) curRow = curRow - 1
If KeyHit(208) curRow = curRow + 1

If curRow > 3 curRow = 3
If curRow < 1 curRow = 1

Flip
Cls

keyVal = GetKey()

If (keyVal>=48 And keyVal<=57) Or (keyVal>=65 And keyVal<=90) Or (keyVal>=97 And keyVal<=122)
Select curRow
Case 1: If Len(myAccount$) < 8 myAccount$ = myAccount$ + Chr(keyVal)
Case 2: If Len(myPassword$) < 12 myPassword$ = myPassword$ + Chr(keyVal)
End Select
End If

If KeyHit(14)
Select curRow
Case 1: If Len(myAccount$) > 0 myAccount$ = Left(myAccount$,Len(myAccount$)-1)
Case 2: If Len(myPassword$) > 0 myPassword$ = Left(myPassword$,Len(myPassword$)-1)
End Select
End If

If KeyHit(28) And curRow = 3 RuntimeError myAccount + "/" + myPassword
Wend


Yahfree(Posted 2007) [#6]
hmm why doesnt this work? just testing this,

AppTitle "Fitting Calculator","Exit Fitting calculator?"

;Include "FunctionLibary.bb"

Graphics 400,300,16,2


POWER=Input("How much powergrid on your ship? ")
Print
CPU=Input("How much CPU on your ship? ")

C_POWER=0
C_CPU=0

ADD_CPU=0
ADD_POWER$=""

While Not KeyHit(1)
Cls


  Text 0,0,"PG: ("+C_POWER+"/"+POWER+")"
  Text 0,15,"CPU: ("+C_CPU+"/"+CPU+")"

  Text 180,80,"Add module:"

Text 0,120,"Power of mod:"
   Rect 180,120,100,20,False

Text 0,150,"CPU of mod:"
   Rect 180,150,100,20,False

Text 0,180,"How many of these?:"
   Rect 180,180,100,20,False

key=GetKey()

If key=>2 And key=<11 ADD_POWER$=ADD_POWER$+Chr(key)

Text 50,50,ADD_POWER$



 Delay 5
Flip
Wend
End


its suppost to output numbers you hit at 50,50...


Yahfree(Posted 2007) [#7]
Weird if i get rid of the conditional part of it "If key=>2 And key=<11" it works, but screams out "|"'s and registers all keys (i only want numbers)

so i screwed up the condition ... looks alright to me? anyone got any ideas?


Yahfree(Posted 2007) [#8]
GRRR SO CLOSE..

Why does yours work but mine doesnt?

this works:

If (key>=48 And key<=57) Or (key>=65 And key<=90) Or (key>=97 And key<=122)


this doesnt:

If (key>=2 And key<=11)



what am i doing wrong?


Yeshu777(Posted 2007) [#9]
Problem is with your use of the value returned by GetKey().

GetKey returns the ascii value.

[edit]

Look at next post....


Yeshu777(Posted 2007) [#10]
Adding to that....

GetKey() returns the following

1 = Home Key
2 = End
3 = Insert
4 = Delete
5 = Page Up
6 = Page Down

Are these the keys you intend on using?

Ascii 49 is number 1, upto 57 which is 9.

Try this...

	key=GetKey()
	
	If(key=>49 And key=<57) Then
		ADD_POWER$=Chr(key)
	EndIf

	Text 50,50, ADD_POWER$

	Delay 5
	key = 0;
	
	
	Flip



Yahfree(Posted 2007) [#11]
Huh? this works but:

Keys "1" thru "0" on the top have the scan codes of "2" thru "11"

i'm confused...


Yahfree(Posted 2007) [#12]
oh i think i see, ASCII codes not scancodes, well whats the "ASCII" code for the number pad? i want to enable this as well.


b32(Posted 2007) [#13]
I don't think the numpad has scancodes. At least, I couldn't find them.
For my program, I used GetKey to make input boxes. Later on, I replace all GetKey's with a custom function (iGetKey) that uses scancodes (keydown) to read the keys.
Here is the code, maybe it helps:



Yahfree(Posted 2007) [#14]
so i copy the functions over and use Igetkey() and not Getkey()?

Edit: Yes it works! thanks for the functions B23 helpful as always :) and thanks lordon and everyone else that helped


Yahfree(Posted 2007) [#15]
Edit: scratch that for now, how do i pull off that flashing cursor effect?

this is what i tried, i thought if you say "Timer=millisecs()" it resets the timer?

why does this not work?

If MilliSecs() < blinky+1000
Select c_textbox
  Case 1 Rect 183+Len(ADD_POWER$)*9,122,1,17,1
  Case 2 Rect 183+Len(ADD_CPU$)*9,152,1,17,1
  Case 3 Rect 183+Len(NO_OFMODS$)*9,182,1,17,1
blinky=MilliSecs()
End Select
End If


should flicker on and off every second?


b32(Posted 2007) [#16]
There is no time defined when the cursor should be 'off'.
The 'on'-time is defined by:
If Millisecs() < blinky+1000

The 'off' time could be defined like this:
If Millisecs() > blinky+2000 then blinky=Millisecs()

Also, the line that resets the timer is in the Select..Case. I think it should be placed after 'End Select'.
When using Millisecs(), I believe it is best to read it at the start of the program's main loop and store it in a variable, because it may have changed between two reads.


Yahfree(Posted 2007) [#17]
Awsome, another quick question, how is it possible to enable the Number pad while prompting with Input()?


Yo! Wazzup?(Posted 2007) [#18]
I don't think you can, but here is a nice input command that someone made:
http://www.blitzbasic.com/Community/posts.php?topic=68736


Yahfree(Posted 2007) [#19]
k, I'll try to develop a function that makes a input field like i have now, but more easy to manage ect. and hides the ugly code


Yahfree(Posted 2007) [#20]
Ok, i'm stumped, why does this not work? its suppost to work like a input box... but for some reason (i think) the string is erasing itself every frame, i cant find where its doing that..




do i have the right idea or am i doing it all wrong? (my first useful function!)

PS. i just put in the Igetkey() ect functions so it works those have nothing to do with the problem (i tested with normal getkey() to make sure!)

the problem lies in the Example, or the "CreateInputBox()" function


Yahfree(Posted 2007) [#21]
This is getting annoying, the reason why its constantly erasing itself is because i'm putting the string i want returned into the function and because its in the function its in the main loop, and it keeps sending that string in empty.. therefore erasing itself.


how on earth do i fix this, this is how i want it to work:


you throw a string veriable in... and when the function is done it throws the string out with the added letters/numbers, these can be used for maths ect, the actal typing onscreen of the userinput is done in the function, but because it keeps sending a empty veriable in...


it breifly shows it (its in the function) then it vanishes (exiting the function, going to the next loop, throwing a empty veriable in it)


I'm confused... what do i do to fix it!!


Yahfree(Posted 2007) [#22]
one way i guess i could fix it is declare a global string before the main loop then use that inside of the function, but then that gets rid of its ability for the user to give it more then one function a Input string name.

limiting them to only using one input field...

Dang i love this function stuff, yet i hate it at the same time, this will be a good learning experince once i figure this out.

Any ideas?


Yeshu777(Posted 2007) [#23]
Post some code and I'll take a look.


Yahfree(Posted 2007) [#24]
look 3 posts above yours


Yeshu777(Posted 2007) [#25]
Try this...

Function CreateInputBox$(box_x,box_y,box_w,box_h,useable,returnstr$)
	
	Rect box_x, box_y, box_w, box_h, False

	If useable=True
	
		key=iGetKey()
	
		Rect box_x+3+Len(returnstr$)*9,box_y+2,1,box_h-3,1
	
		If (key=>48 And key=<57) Or (key>=65 And key<=90) Or (key>=97 And key<=122)

			If(Len(returnstr$)<11)

				returnstr$ = returnstr$ + Chr(key)
				
			End If

		End If
		
	End If

	Text box_x+3,box_y, returnstr$

	Return(returnstr$); This bit updates your string
		
End Function


And then change your main loop to...

mystring$=""

While Not KeyHit(1)

	Cls

	mystring$ = CreateInputBox(0,0,100,20,True,mystring$)
	
	Delay 5

	Flip

	Wend
End



Yahfree(Posted 2007) [#26]
:) THANKS!!! that was interesting to say the least...

i'll release the function in the code archives once i'm done putting the finishing touchs on it


Yahfree(Posted 2007) [#27]
grr... this is wierd i'm trying to control what inputbox is active with a case select thingy:

;-----------------------------------------------------------------------------------------------------
;											   Types
;-----------------------------------------------------------------------------------------------------
	
;type to hold scancodes
Type ScanCode
	Field code
	Field key$
	Field upkey$
End Type

;-----------------------------------------------------------------------------------------------------
;											InitGetKey()
;-----------------------------------------------------------------------------------------------------
;reads all scancodes into ScanCode type
Function InitGetKey()

	;read scancodes
	tel = 1
	Restore scancodez
	Repeat
		Read scanc
		If scanc = -1 Then Exit
		s.ScanCode = New ScanCode
		s\code = scanc
		s\key$ = Lower$(Mid$("1234567890-=QWERTYUIOP[]ASDFGHJKL;'\ZXCVBNM,./* 789-456+1230.,/", tel, 1))
		s\upkey$ = Mid$("!@#$%^&*()_+QWERTYUIOP{}ASDFGHJKL:" + Chr$(34) + "|ZXCVBNM<>?* 789-456+1230.,/", tel, 1)
		tel = tel + 1
	Forever

End Function

Global oldkdown
;-----------------------------------------------------------------------------------------------------
;											iGetKey()
;-----------------------------------------------------------------------------------------------------
;imitates GetKey() using scancodes
Function iGetKey()

	;backspace
	If KeyDown(14) Then 
		If oldkdown <> 8 Then oldkdown = 8: Return 8
		oldkdown = 8: Return
	End If

	;enter
	If KeyDown(28) Or KeyDown(156) Then 
		If oldkdown <> 13 Then oldkdown = 13: Return 13
		oldkdown = 13: Return
	End If
	
	;check what key is down
	sel.ScanCode = Null
	down = -1
	For s.ScanCode = Each ScanCode
		If KeyDown(s\code) Then down = s\code: sel = s: Exit
	Next
	If down = oldkdown Then Return
	oldkdown = down

	;if no valid key is selected, exit	
	If sel = Null Then Return 0

	;shift for uppercase	
	If KeyDown(42) Or KeyDown(54) Then
		sc$ = sel\upkey$
	Else
		sc$ = sel\key$
	End If

	;return ascii value of key	
	Return Asc(sc$)

End Function


;-------------------------------------------------------------------------------------------------------
;                                          CreateInputBox()
;-------------------------------------------------------------------------------------------------------
;Function CreateInputBox(box_x,box_y,box_w,useable)


;Rect box_x,box_y,box_w,20,False

;If useable=True
 ;  key=iGetKey()
 ;  Rect box_x+3+Len(returnstr$)*9,box_y+2,1,17,1
 ; If (key=>48 And key=<57) Or (key>=65 And key<=90) Or (key>=97 And key<=122)
 ;    If Len(returnstr$)<box_w/10 returnstr$=returnstr$+Chr(key)     
;End If
;  End If

;If useable=True
;  If KeyHit(14)
;     If Len(returnstr$) > 0 returnstr$ = Left(returnstr$,Len(returnstr$)-1)  
;End If
 ; End If


;Text box_x+3,box_y,returnstr$


;Return returnstr$

;End Function


Function CreateInputBox$(box_x,box_y,box_w,useable,returnstr$)
	
	Rect box_x, box_y, box_w,20, False

	While useable=True
	
		key=iGetKey()
	
      Rect box_x+3+Len(returnstr$)*9,box_y+2,1,17,1
		
	
		If (key=>48 And key=<57) Or (key>=65 And key<=90) Or (key>=97 And key<=122)

			If(Len(returnstr$)<11)

				returnstr$ = returnstr$ + Chr(key)
				
				
				End If
			End If


		If KeyHit(14) 
				 If Len(returnstr$)>0 Left(returnstr,Len(returnstr)-1)

End If
	Text box_x+3,box_y, returnstr$

	Return(returnstr$)
	
	Wend
		
End Function



;-------------------------------------------------------------------------------------------------------
                                       ;Data stuff
;-------------------------------------------------------------------------------------------------------
.scancodez
Data 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Data 12, 13, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
Data 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 38
Data 39, 40, 43, 44, 45, 46, 47, 48, 49, 50, 51
Data 52, 53, 55, 57, 71, 72, 73, 74, 75, 76, 77
Data 78, 79, 80, 81, 82, 83, 179, 181, -1


;----------------------------------------------
;                    demo
;----------------------------------------------

;Include "FunctionLibary.bb"

Graphics 400,300,16,2
SetBuffer BackBuffer()

InitGetKey()

mystring$=""
mystring2$=""
mystring3$=""

tf=False
tf2=False
tf3=False

controller=1

Global blinky=MilliSecs()

While Not KeyHit(1)
Cls

;CreateInputBox(box_x,box_y,box_w,box_h,useable,letters,strng$)
mystring = CreateInputBox(0,0,300,True,mystring$)
mystring2=CreateInputBox(0,30,300,False,mystring2$)
mystring3=CreateInputBox(0,60,300,False,mystring3$)


Select controller
Case 1 tf=True
Case 2 tf2=False
Case 3 tf3=False

Default tf=False tf2=False tf3=False 

End Select


If KeyHit(200) controller=controller+1
If KeyHit(208) controller=controller-1

If controller > 3 controller=1
If controller < 1 controller=3


Text 0,350,controller



Delay 5
Flip
Wend
End



why doesnt that work? it looks perfectly fine to me


Yahfree(Posted 2007) [#28]
got it to work, stupid me lol

what the input archive i'll be adding this shortly.


b32(Posted 2007) [#29]
Hey, I've updated the routine, since I noticed that it sometimes had trouble handling the right order of the keys if people type too fast. So I thought, I'd post it:



Yahfree(Posted 2007) [#30]
edit nvm i read wrong..


Nice thanks for that