Code archives/User Input/ASynchronous Input Function

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

Download source code

ASynchronous Input Function by Pantheon2007
I almost feel cheeky posting something so simple but i think you will find this solution is quite elegant. Its also something that everyone will have to write at some stage in there game, so...

This function will modify the input string based on the keyboard events. It will handle all case text, symbols and numbers. Anything it doesnt know it will return the ascii code of, usefull for mapping the escape or tab key.

I was writing this for the GUI im working and I like the idea of sharing code, have fun.
'/**
' * ASYNCHRONOUS INPUT USING POLLED INPUT
' * 
' *   this code will change the input string based
' *   on the current keyboard events, and is usefull
' *   for non blocking input
' *   return codes to handle special events
' *
' *	  these must be larger than a byte	  
' *
Const IAS_Normal	= $AAAAAAAA	'we're sweet
Const IAS_Return	= $BBBBBBBB	'carrage return hit
' *
' *   if anything else was returned then its the
' *   ASCII value of the unknown key press
' *
' *	  tab is at ascii <9>
' *   escape is at ascii <27>
' *
Function InputASync:Int( aStr:String Var )
	Local a:Int = GetChar( )
	' loop hack :)
	Repeat
		' * space numbers symbols letters
		If (a=>32 And a<=126) Then Exit
		' * pound symbol
		If (a=163) Then Exit
		' * carrage return
		If (a=13) Then Return IAS_Return
		' * backspace key
		If (a=8)
			Local b:Int = Len( aStr )
			If (b>0) Then aStr = Left( aStr, b-1 )
			Return IAS_Normal
		EndIf
		' * no input entered
		If (a=0) Then Return IAS_Normal
		' something else cought
		Return a
	Forever
	aStr = aStr+Chr( a )
	Return IAS_Normal
End Function
' *
' */


'/**
' * EXAMPLE CODE
' *
' *		just to show why its a bit different ;)
' */
Local myString:String = "type 'exit' to close!"
Local cursor:String = ""
Local time = 0
Graphics 320, 240, 0
Repeat
	Cls
	
	Local ret:Int = InputASync( myString )
	
	' what did we get back
	Select( ret )
	Case IAS_Normal
		' hack to warp between 1 and 360 for the sin wave
		time :+ ( 1- (time > 360) *360 )
		
	Case IAS_Return
		' what have we got
		If ( Lower( myString ) = "exit" ) Then Exit
		' no? well just erase
		myString = ""
		
	Default
		' found an undisplayable ascii value
		myString = "ASCII value <"+ret+"> not handled!"
		
		' is it the escape key
		If (ret = 27) Then Exit
		
	End Select
	
	' make a blinking cursor
	If ((time Mod 40) < 20)
		cursor = "_"
	Else
		cursor = ""
	EndIf
	
	' draw our string plus cursor
	DrawText myString+cursor, 10, 40+Sin( time )*20
	
	Flip
Forever

Comments

Pantheon2007
I just realised that this will only handle one charictor every time its called but its not very hard to modify to deal with them all so i mite update it but if not then its a user exercise ;) If its even necessary.


Code Archives Forum