News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Custom Input

Started by kevin, October 26, 2004, 03:32:51 PM

Previous topic - Next topic

kevin

Here a 'real time' scancode based input routine.   You might notice The input function doesn't need any other globals/arrays, this is because it keeps track of things itself, by using static variables.  

Another cool thing, is the function returns two bits of data,  The current line of input text and the return  status of this input.   Status will be TRUE when user hits ENTER key..  

 Since the function polls input,  you can thread the routine into your game without stopping it..



PlayBASIC Code: [Select]
; Call the custom input routine with a init flag of 0
CustomInput(100,100,0)

Repeat
Cls 0

Mytext$,Status=CustomInput(200,200,1)

; Display the Screen and wait for the user to press a key
Sync
Until Status=True

SetCursorY 250

Print "Input Complete - Here is your text"
Print MyText$

Sync
WaitNoKey
WaitKey
END




Function CustomInput(xpos,ypos,Flag)

Static CursorTimer,CursorPos,Cursor$,MyText$,LastScanCode,KeyDownTimer

; Is the user passes this function a Zero it wil reset the Static variables
; within
If Flag=0
MyText$=""
CursorPos=0
Cursor$="_____ "
CursorTimer=0
CursorPos=0
LastScanCode=0
KeyDownTimer=0
EndIf


; Handle the Cursor Frame

CursorFrame$=Mid$(cursor$,cursorpos,1)
If CursorTimer<Timer()
CursorTimer=Timer()+20
INC cursorPos
If Cursorpos > (Len(cursor$)-1) Then CursorPos=0
EndIf

; Grab the Scan code
ThisKey=ScanCode()

; Strip off the Shift bits
ThisKey2=ThisKey And 255

; Check if the Key is still being pressed
If ThisKey2=LastScancode
If KeyDownTimer=0
KeyDownTimer=Timer()+500
Goto abort
Else
If Timer()<KeyDownTimer Then Goto Abort
KeyDownTimer=Timer()+50
EndIf
Else
KeyDownTimer=0
EndIf
LastScanCode=ThisKey2


; Check if the SHIFT BITS are set in the scancode value
ShiftState= (ThisKey And 512+256)<>0

; Clear the Shoft bits from the key value
ThisKey=ThisKey And 255

; Handle this scan code
Select ThisKey
Case 2 To 13
If ShiftState=0
s$=Mid$("1234567890-=",(ThisKey-1),1)
Else
s$=Mid$("!@#$%^&*()_+",(ThisKey-1),1)
EndIf
; Handle lower case Keys with no shift
Case 16 To 27
If ShiftState=0
s$=Mid$("qwertyuiop[]",(ThisKey-15),1)
Else
s$=Mid$("QWERTYUIOP{}",(ThisKey-(15)),1)
EndIf
Case 30 To 40
If ShiftState=0
s$=Mid$("asdfghjkl;'",(ThisKey-29),1)
Else
s$=Mid$("ASDFGHJKL:"+Chr$(34),(ThisKey-29),1)
EndIf
Case 43 To 53
If ShiftState=0
s$=Mid$("\zxcvbnm,./",(ThisKey-42),1)
Else
s$=Mid$("|ZXCVBNM<>?",(ThisKey-42),1)
EndIf

// Handle Delete / Backspace keys
Case 14,212
If Len(Mytext$)>0 Then Mytext$=CutRight$(Mytext$,Len(mytext$))

// Handle SPACE
Case 57
s$=" "
// Handle Enter
Case 28
Status=1
EndSelect

Abort:

If s$<>"" Then MyText$=MyText$+s$
Text Xpos,Ypos,Mytext$+CursorFrame$
EndFunction MyText$, Status