Code archives/User Input/Text input

This code has been declared by its author to be Public Domain code.

Download source code

Text input by lonnieh2005
Heres a text input type that I've hacked up, with blinking cursor. Just need to call Update once in your loop. EDIT: You may need to set your orientation/alpha either before you call the update method or by adding it into the type itself
Graphics 640, 480

Local text:GInput = GInput.Create(10, 30, ":-D ")

While Not KeyHit(KEY_ESCAPE)
	Cls
	DrawText "Check it out. Press [ESC] to exit.", 10, 10
	text.Update
	Flip
	FlushMem
Wend
	
Type GInput
	Field text:String, enabled:Int = True
	Field width:Int, cursor:Int = False, x:Int, y:Int, time:Long = MilliSecs()
	
	Method Update()
		If enabled Then
			Local q:Int = GetChar()
			If q > 0 Then
				Select q
					Case KEY_BACKSPACE
						text = Left(text, Len(text) - 1)
					Case KEY_ENTER
						'I don't know what you'd want to do with this, so I
						'left it blank, perhaps its time for function pointers.
					Case KEY_TAB
						text:+ "     "
					Default
						If Not KeyDown(KEY_ALT) And Not KeyDown(KEY_CONTROL) Then text:+ Chr(q)
				End Select
				width = TextWidth(text)
				cursor = True
				time = MilliSecs()
			End If
			If MilliSecs() > time + 400 Then
				cursor = Not cursor
				time = MilliSecs()
			End If
			If cursor Then
				DrawLine x + width, y + 1, x + width, y + TextHeight("1") - 1
			End If
		End If
		DrawText text, x, y
	End Method
	
	Function Create:GInput(x:Int, y:Int, text:String = "")
		Local ret:GInput = New GInput
		ret.text = text
		ret.x = x
		ret.y = y
		ret.width = TextWidth(text)
		Return ret
	End Function
End Type

Comments

Sanctus2006
Thx


Code Archives Forum