October 28, 2020, 06:34:22 AM

Author Topic: [bmx] ASynchronous Input Function by Pantheon [ 1+ years ago ]  (Read 1080 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : ASynchronous Input Function
Author : Pantheon
Posted : 1+ years ago

Description : 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.


Code :
Code: BlitzMax
  1. '/**
  2. ' * ASYNCHRONOUS INPUT USING POLLED INPUT
  3. ' *
  4. ' *   this code will change the input string based
  5. ' *   on the current keyboard events, and is usefull
  6. ' *   for non blocking input
  7. ' *   return codes to handle special events
  8. ' *
  9. ' *       these must be larger than a byte       
  10. ' *
  11. Const IAS_Normal        = $AAAAAAAA     'we're sweet
  12. Const IAS_Return        = $BBBBBBBB     'carrage return hit
  13. ' *
  14. ' *   if anything else was returned then its the
  15. ' *   ASCII value of the unknown key press
  16. ' *
  17. ' *       tab is at ascii <9>
  18. ' *   escape is at ascii <27>
  19. ' *
  20. Function InputASync:Int( aStr:String Var )
  21.         Local a:Int = GetChar( )
  22.         ' loop hack :)
  23.         Repeat
  24.                 ' * space numbers symbols letters
  25.                 If (a=>32 And a<=126) Then Exit
  26.                 ' * pound symbol
  27.                 If (a=163) Then Exit
  28.                 ' * carrage return
  29.                 If (a=13) Then Return IAS_Return
  30.                 ' * backspace key
  31.                 If (a=8)
  32.                         Local b:Int = Len( aStr )
  33.                         If (b>0) Then aStr = Left( aStr, b-1 )
  34.                         Return IAS_Normal
  35.                 EndIf
  36.                 ' * no input entered
  37.                 If (a=0) Then Return IAS_Normal
  38.                 ' something else cought
  39.                 Return a
  40.         Forever
  41.         aStr = aStr+Chr( a )
  42.         Return IAS_Normal
  43. End Function
  44. ' *
  45. ' */
  46.  
  47.  
  48. '/**
  49. ' * EXAMPLE CODE
  50. ' *
  51. ' *             just to show why its a bit different ;)
  52. ' */
  53. Local myString:String = "type 'exit' to close!"
  54. Local cursor:String = ""
  55. Local time = 0
  56. Graphics 320, 240, 0
  57. Repeat
  58.         Cls
  59.        
  60.         Local ret:Int = InputASync( myString )
  61.        
  62.         ' what did we get back
  63.         Select( ret )
  64.         Case IAS_Normal
  65.                 ' hack to warp between 1 and 360 for the sin wave
  66.                 time :+ ( 1- (time > 360) *360 )
  67.                
  68.         Case IAS_Return
  69.                 ' what have we got
  70.                 If ( Lower( myString ) = "exit" ) Then Exit
  71.                 ' no? well just erase
  72.                 myString = ""
  73.                
  74.         Default
  75.                 ' found an undisplayable ascii value
  76.                 myString = "ASCII value <"+ret+"> not handled!"
  77.                
  78.                 ' is it the escape key
  79.                 If (ret = 27) Then Exit
  80.                
  81.         End Select
  82.        
  83.         ' make a blinking cursor
  84.         If ((time Mod 40) < 20)
  85.                 cursor = "_"
  86.         Else
  87.                 cursor = ""
  88.         EndIf
  89.        
  90.         ' draw our string plus cursor
  91.         DrawText myString+cursor, 10, 40+Sin( time )*20
  92.        
  93.         Flip
  94. Forever


Comments :


Pantheon(Posted 1+ years ago)

 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.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal