function joystickkeyboard$(drawx1,drawy1,capson,chmax,usesprite1,useimg1,usetext1,cf$)
LOCAL original$

//this should be here to clear joystick and button presses on start
sync()
nf$=left(cf$,chmax)
cf$=nf$
original$=cf$
//do the keys lineup?  adjust these numbers to center them if they do not
kbalignmenty1=2
kbalignmenty2=0
kbalignmenty3=0
kbalignmenty4=0
kbalignmenty5=0
kbalignmenty6=0


prioritykb=1
olddrawy=drawy1

//do you want keyboard to scroll up on screen??
scroll1=800  //start og graphic screen (should be farther down then drawy1)
drawy1=scroll1  //this
//scroll1=drawy1  //or that  (does not scroll and fixes on screen)




alpha=200
setfolder("")
setfolder("media")
//loadimage(3,"bg.png",1)
loadimage(useimg1,"data/key1.png",0)
//loadimage(2,"key2.png",1)

createsprite(usesprite1,useimg1)
setspriteposition(usesprite1,drawx1,drawy1)
setspritedepth(usesprite1,prioritykb)
setspritecolor(usesprite1,100,100,100,alpha)
setspritecoloralpha(usesprite1,alpha)
//lets make the letters
txt1=usetext1


//top row
createtext(txt1,"ESC")
settextsize(txt1,20)
settextposition(txt1,drawx1+37,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"1")
settextsize(txt1,20)
settextposition(txt1,drawx1+115,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"2")
settextsize(txt1,20)
settextposition(txt1,drawx1+180,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"3")
settextsize(txt1,20)
settextposition(txt1,drawx1+245,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"4")
settextsize(txt1,20)
settextposition(txt1,drawx1+308,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"5")
settextsize(txt1,20)
settextposition(txt1,drawx1+372,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"6")
settextsize(txt1,20)
settextposition(txt1,drawx1+438,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"7")
settextsize(txt1,20)
settextposition(txt1,drawx1+503,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"8")
settextsize(txt1,20)
settextposition(txt1,drawx1+565,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"9")
settextsize(txt1,20)
settextposition(txt1,drawx1+630,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"0")
settextsize(txt1,20)
settextposition(txt1,drawx1+693,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"_")
settextsize(txt1,20)
settextposition(txt1,drawx1+760,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"=")
settextsize(txt1,20)
settextposition(txt1,drawx1+825,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"Delete")
settextsize(txt1,20)
settextposition(txt1,drawx1+890,drawy1+85+kbalignmenty2)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)


//start second row
txt1=txt1+1
createtext(txt1,"@")
settextsize(txt1,20)
settextposition(txt1,drawx1+65,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"Q")
settextsize(txt1,20)
settextposition(txt1,drawx1+147,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"W")
settextsize(txt1,20)
settextposition(txt1,drawx1+210,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"E")
settextsize(txt1,20)
settextposition(txt1,drawx1+275,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"R")
settextsize(txt1,20)
settextposition(txt1,drawx1+340,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"T")
settextsize(txt1,20)
settextposition(txt1,drawx1+405,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"Y")
settextsize(txt1,20)
settextposition(txt1,drawx1+470,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"U")
settextsize(txt1,20)
settextposition(txt1,drawx1+535,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"I")
settextsize(txt1,20)
settextposition(txt1,drawx1+600,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"O")
settextsize(txt1,20)
settextposition(txt1,drawx1+665,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"P")
settextsize(txt1,20)
settextposition(txt1,drawx1+730,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"[")
settextsize(txt1,20)
settextposition(txt1,drawx1+792,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"]")
settextsize(txt1,20)
settextposition(txt1,drawx1+857,drawy1+132+kbalignmenty3)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)


//third row
txt1=txt1+1
createtext(txt1,"Caps ON")
settextsize(txt1,20)
settextposition(txt1,drawx1+37,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"A")
settextsize(txt1,20)
settextposition(txt1,drawx1+163,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"S")
settextsize(txt1,20)
settextposition(txt1,drawx1+227,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"D")
settextsize(txt1,20)
settextposition(txt1,drawx1+293,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"F")
settextsize(txt1,20)
settextposition(txt1,drawx1+358,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"G")
settextsize(txt1,20)
settextposition(txt1,drawx1+420,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"H")
settextsize(txt1,20)
settextposition(txt1,drawx1+485,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"J")
settextsize(txt1,20)
settextposition(txt1,drawx1+550,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"K")
settextsize(txt1,20)
settextposition(txt1,drawx1+615,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"L")
settextsize(txt1,20)
settextposition(txt1,drawx1+680,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,";")
settextsize(txt1,20)
settextposition(txt1,drawx1+745,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"'")
settextsize(txt1,20)
settextposition(txt1,drawx1+810,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"Enter")
settextsize(txt1,20)
settextposition(txt1,drawx1+888,drawy1+177+kbalignmenty4)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)


//next line
txt1=txt1+1
createtext(txt1,"SHIFT=CAPS")
settextsize(txt1,20)
settextposition(txt1,drawx1+35,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"Z")
settextsize(txt1,20)
settextposition(txt1,drawx1+195,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"X")
settextsize(txt1,20)
settextposition(txt1,drawx1+260,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"C")
settextsize(txt1,20)
settextposition(txt1,drawx1+325,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"V")
settextsize(txt1,20)
settextposition(txt1,drawx1+390,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"B")
settextsize(txt1,20)
settextposition(txt1,drawx1+455,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"N")
settextsize(txt1,20)
settextposition(txt1,drawx1+518,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"M")
settextsize(txt1,20)
settextposition(txt1,drawx1+582,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,",")
settextsize(txt1,20)
settextposition(txt1,drawx1+647,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,".")
settextsize(txt1,20)
settextposition(txt1,drawx1+712,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"/")
settextsize(txt1,20)
settextposition(txt1,drawx1+775,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,".com")
settextsize(txt1,20)
settextposition(txt1,drawx1+875,drawy1+225+kbalignmenty5)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)

//final row
txt1=txt1+1
createtext(txt1,"www.")
settextsize(txt1,20)
settextposition(txt1,drawx1+48,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"CTR")
settextsize(txt1,20)
settextposition(txt1,drawx1+135,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"ALT")
settextsize(txt1,20)
settextposition(txt1,drawx1+215,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"Space Bar")
settextsize(txt1,20)
settextposition(txt1,drawx1+390,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,255,255,255)
txt1=txt1+1
createtext(txt1,"CTR")
settextsize(txt1,20)
settextposition(txt1,drawx1+620,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"ALT")
settextsize(txt1,20)
settextposition(txt1,drawx1+700,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"\")
settextsize(txt1,20)
settextposition(txt1,drawx1+793,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"|")
settextsize(txt1,20)
settextposition(txt1,drawx1+858,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)
txt1=txt1+1
createtext(txt1,"~")
settextsize(txt1,20)
settextposition(txt1,drawx1+938,drawy1+272+kbalignmenty6)
settextdepth(txt1,prioritykb)
settextcolor(txt1,50,50,50,255)


//done
txt1=txt1+1
createtext(txt1,"|")
settextsize(txt1,20)
settextposition(txt1,drawx1+20,drawy1+8+kbalignmenty1)
settextdepth(txt1,prioritykb)
settextcolor(txt1,255,0,0,255)
//flasher
flash=txt1

//actual text string
txt1=txt1+1
createtext(txt1,"")
settextsize(txt1,20)
settextposition(txt1,drawx1+20,drawy1+8+kbalignmenty1)
settextdepth(txt1,prioritykb)
settextcolor(txt1,0,0,255,255)
mytextstring$=""
actualstring=txt1

maxresources=txt1

rem A Wizard Did It!
myflash=0
flashit=1

currentbutton=usetext1

slidey1#=0
slidex1#=0
row=1
button$=""

if capson=0
    capson=1
    rp=adjustalpha(capson,usetext1)
    capson=rp
endif

drawy1=olddrawy


if cf$<>"" then mytextstring$=cf$

do
//print(getrawmousex()-drawx1)
//print(getrawmousey()-drawy1)



if getspritey(usesprite1)>drawy1
    yy=getspritey(usesprite1)
    setspriteposition(usesprite1,drawx1,yy-20)
    for tt=usetext1 to maxresources step 1
        if gettextexists(tt)=1
            yy=gettexty(tt)
            settextposition(tt,gettextx(tt),yy-20)
        endif
    next tt
endif



    myflash=myflash+1
    if myflash=>30
        myflash=0
        flashit=flashit+1
        if flashit=>2 then flashit=0

        //flash my current key that im on
        if flashit=0 then settextcolor(currentbutton,255,0,0,255)
        if flashit=1 then settextcolor(currentbutton,0,255,255,255)

        //position flasher in correct spot
        vv=len(mytextstring$)
        settextstring(actualstring,mytextstring$)
        mkstr$=""
        for y=1 to vv step 1
            mkstr$=mkstr$+" "
        next y
        mkstr$=mkstr$+"|"
        settextstring(flash,mkstr$)


        if flashit=0
            settextcolor(flash,255,255,255,0)
        endif
        if flashit=1
            settextcolor(flash,255,0,0,255)
        endif
    endif



    //check for joystick input
    for t=1 to 4 step 1
        if getrawjoystickexists(t)=1

            //navigation
            xx#=getrawjoystickx(t)
            yy#=getrawjoysticky(t)

                slidey1#=slidey1#-yy#
                slidex1#=slidex1#+xx#

                if slidex1#=>10
                    slidex1#=slidex1#-10
                    myflash=31
                    flashit=1
                    if row=1

                        if currentbutton=usetext1+11
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton+2
                        endif
                        if currentbutton<usetext1+11
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton+1
                        endif



                    endif

                    if row=2
                        if currentbutton<usetext1+24
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton+1
                        endif



                    endif

                    if row=3
                        if currentbutton=usetext1+36
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton+3
                        endif
                        if currentbutton>usetext1+26 and currentbutton=<usetext1+35
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton+1
                        endif
                    endif

                    if row=4
                        if currentbutton<usetext1+47
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton+1
                        endif
                    endif
                endif


                if slidex1#=<-10
                    slidex1#=slidex1#+10
                    myflash=31
                    flashit=1
                    if row=1
                        if currentbutton>usetext1 and currentbutton<>usetext1+13
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton-1
                        endif
                        if currentbutton=usetext1+13
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton-2
                        endif
                    endif

                    if row=2
                        if currentbutton>usetext1+15
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton-1
                        endif
                    endif

                    if row=3
                        if currentbutton>usetext1+27 and currentbutton=<usetext1+36
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton-1
                        endif
                        if currentbutton=usetext1+39
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton-3
                        endif
                    endif

                    if row=4
                        if currentbutton>usetext1+41
                            settextcolor(currentbutton,0,255,255,255)
                            currentbutton=currentbutton-1
                        endif
                    endif
                endif

               if slidey1#=>10
                    slidey1#=slidey1#-10
                    myflash=31
                    flashit=1

                    if row=2
                        settextcolor(currentbutton,0,255,255,255)
                        //if row=2
                        settextcolor(currentbutton,0,255,255,255)
                        row=1
                        currentbutton=currentbutton-14
                        if currentbutton<usetext1 then currentbutton=usetext1
                        if currentbutton>usetext1+13 then currentbutton=usetext1+13
                        if currentbutton=usetext1+12 then currentbutton=usetext1+11
                        //endif
                    endif

                    if row=3
                        settextcolor(currentbutton,0,255,255,255)
                        //if row=2
                        row=2
                        currentbutton=currentbutton-14
                        if currentbutton<usetext1+15 then currentbutton=usetext1+15
                        if currentbutton>usetext1+24 then currentbutton=usetext1+24
                        //endif
                    endif
                    if row=4
                        settextcolor(currentbutton,0,255,255,255)
                        //if row=2
                        row=3
                        currentbutton=currentbutton-13
                        //if currentbutton<usetext1+15 then currentbutton=usetext1+15
                        //if currentbutton>usetext1+24 then currentbutton=usetext1+24
                        //endif
                    endif

                    if row=5
                        settextcolor(currentbutton,0,255,255,255)
                        row=4
                        currentbutton=currentbutton-10
                        //if currentbutton=<usetext1+41 then currentbutton=usetext1+41
                        //if currentbutton=>usetext1+47 then currentbutton=usetext1+47
                    endif

               endif

             if slidey1#=<-10
                    slidey1#=slidey1#+10
                    myflash=31
                    flashit=1
                    if row=4
                        settextcolor(currentbutton,0,255,255,255)
                        row=5
                        currentbutton=usetext1+55
                        //if currentbutton=<usetext1+41 then currentbutton=usetext1+41
                        //if currentbutton=>usetext1+47 then currentbutton=usetext1+47
                    endif
                    if row=3
                        settextcolor(currentbutton,0,255,255,255)
                        row=4
                        currentbutton=currentbutton+13
                        if currentbutton=<usetext1+41 then currentbutton=usetext1+41
                        if currentbutton=>usetext1+47 then currentbutton=usetext1+47
                    endif

                    if row=2
                        settextcolor(currentbutton,0,255,255,255)
                        row=3
                        currentbutton=currentbutton+13
                        if currentbutton=<usetext1+27 then currentbutton=usetext1+27
                        if currentbutton=>usetext1+37 then currentbutton=usetext1+39
                    endif

                    if row=1
                        settextcolor(currentbutton,0,255,255,255)
                        row=2
                        currentbutton=currentbutton+14
                        if currentbutton=<usetext1+15 then currentbutton=usetext1+15
                        if currentbutton>usetext1+24 then currentbutton=usetext1+24
                    endif


               endif

                if getrawjoystickbuttonreleased(t,2)=1 or getrawjoystickbuttonreleased(t,3)=1
                //backspace command
                    //lets delete a key
                    myflash=31
                    flashit=1
                    vv=len(mytextstring$)
                    if vv=1
                        mytextstring$=""
                    endif
                    if vv>1
                        newstring$=left(mytextstring$,vv-1)
                        mytextstring$=newstring$
                    endif
                endif

                if getrawjoystickbuttonreleased(t,4)=1
                    //quick caps
                    rp=adjustalpha(capson,usetext1)
                    capson=rp
                endif

                if getrawjoystickbuttonreleased(t,8)=1
                    //delete resources
                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        sync()
                    endwhile

                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    if mytextstring$="" then mytextstring$="null"
                    exitfunction mytextstring$
                    //exit function return string
                endif


            if getrawjoystickbuttonreleased(t,1)=1
                //entered a command

                //escape key was pressed
                button$=grabkey$(currentbutton,usetext1,capson)
                //print(button$)
                if button$="ESC"
                    //delete resources

                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        sync()
                    endwhile



                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    exitfunction original$
                    //exit function canceled
                endif

                if button$="ENTER"
                    //delete resources

                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        
                        sync()
                    endwhile

                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    if mytextstring$="" then mytextstring$="null"
                    exitfunction mytextstring$
                    //exit function return string
                endif


                if button$="CAPS"
                    rp=adjustalpha(capson,usetext1)
                    capson=rp
                endif

                if button$="DEL"
                    //lets delete a key
                    vv=len(mytextstring$)
                    if vv=1
                        mytextstring$=""
                    endif
                    if vv>1
                        newstring$=left(mytextstring$,vv-1)
                        mytextstring$=newstring$
                    endif

                endif

                //is it a key of interest?
                if button$<>"DEL" and button$<>"ESC" and button$<>"" and button$<>"ENTER" and button$<>"CAPS"
                    vv=len(mytextstring$)
                    if vv<chmax
                        mytextstring$=mytextstring$+button$
                    endif

                endif

                //fix it
                settextstring(actualstring,mytextstring$)
                vv=len(mytextstring$)
                //settextstring(actualstring,mytextstring$)
                mkstr$=""
                for y=1 to vv step 1
                    mkstr$=mkstr$+" "
                next y
                mkstr$=mkstr$+"|"
                settextstring(flash,mkstr$)
            endif
        endif
    next t

    //check for actual keyboard keys input
    if getkeyboardexists()=1
        //b=GetRawLastKey( )
        //print(b)

        //13=enter
        if getrawkeyreleased(13)=1
                    //delete resources
                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        sync()
                    endwhile
                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    if mytextstring$="" then mytextstring$="null"
                    exitfunction mytextstring$
                    //exit function return string
        endif

        //8 =backspace
        if getrawkeyreleased(8)=1
        //lets delete a key
                    vv=len(mytextstring$)
                    if vv=1
                        mytextstring$=""
                    endif
                    if vv>1
                        newstring$=left(mytextstring$,vv-1)
                        mytextstring$=newstring$
                    endif
            myflash=31
            flashit=1
        endif

        //16 =shift (use for caps lock)
        if getrawkeyreleased(16)=1
            rp=adjustalpha(capson,usetext1)
            capson=rp
            myflash=31
            flashit=1
        endif

        //esc key
        //delete resources
        if getrawkeyreleased(27)=1
                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        sync()
                    endwhile
                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    exitfunction original$
                    //exit function canceled
        endif

        //top row
        keypressed99$=""
        if getrawkeyreleased(49)=1 then keypressed99$="1"
        if getrawkeyreleased(50)=1 then keypressed99$="2"
        if getrawkeyreleased(51)=1 then keypressed99$="3"
        if getrawkeyreleased(52)=1 then keypressed99$="4"
        if getrawkeyreleased(53)=1 then keypressed99$="5"
        if getrawkeyreleased(54)=1 then keypressed99$="6"
        if getrawkeyreleased(55)=1 then keypressed99$="7"
        if getrawkeyreleased(56)=1 then keypressed99$="8"
        if getrawkeyreleased(57)=1 then keypressed99$="9"
        if getrawkeyreleased(48)=1 then keypressed99$="0"
        if getrawkeyreleased(189)=1 and capson=1 then keypressed99$="_"
        if getrawkeyreleased(189)=1 and capson=0 then keypressed99$="-"
        //secondrow
        if getrawkeyreleased(81)=1
            if capson=1 then keypressed99$="Q" else keypressed99$="q"
        endif
        if getrawkeyreleased(87)=1
            if capson=1 then keypressed99$="W" else keypressed99$="w"
        endif
        if getrawkeyreleased(69)=1
            if capson=1 then keypressed99$="E" else keypressed99$="e"
        endif
        if getrawkeyreleased(82)=1
            if capson=1 then keypressed99$="R" else keypressed99$="r"
        endif
        if getrawkeyreleased(84)=1
            if capson=1 then keypressed99$="T" else keypressed99$="t"
        endif
        if getrawkeyreleased(89)=1
            if capson=1 then keypressed99$="Y" else keypressed99$="y"
        endif
        if getrawkeyreleased(85)=1
            if capson=1 then keypressed99$="U" else keypressed99$="u"
        endif
        if getrawkeyreleased(73)=1
            if capson=1 then keypressed99$="I" else keypressed99$="i"
        endif
        if getrawkeyreleased(79)=1
            if capson=1 then keypressed99$="O" else keypressed99$="o"
        endif
        if getrawkeyreleased(80)=1
            if capson=1 then keypressed99$="P" else keypressed99$="p"
        endif
       //thirdrow
        if getrawkeyreleased(65)=1
            if capson=1 then keypressed99$="A" else keypressed99$="a"
        endif
        if getrawkeyreleased(83)=1
            if capson=1 then keypressed99$="S" else keypressed99$="s"
        endif
        if getrawkeyreleased(68)=1
            if capson=1 then keypressed99$="D" else keypressed99$="d"
        endif
        if getrawkeyreleased(70)=1
            if capson=1 then keypressed99$="F" else keypressed99$="f"
        endif
        if getrawkeyreleased(71)=1
            if capson=1 then keypressed99$="G" else keypressed99$="g"
        endif
        if getrawkeyreleased(72)=1
            if capson=1 then keypressed99$="H" else keypressed99$="h"
        endif
        if getrawkeyreleased(74)=1
            if capson=1 then keypressed99$="J" else keypressed99$="j"
        endif
        if getrawkeyreleased(75)=1
            if capson=1 then keypressed99$="K" else keypressed99$="k"
        endif
        if getrawkeyreleased(76)=1
            if capson=1 then keypressed99$="L" else keypressed99$="l"
        endif

      //fourthrow
        if getrawkeyreleased(90)=1
            if capson=1 then keypressed99$="Z" else keypressed99$="z"
        endif
        if getrawkeyreleased(88)=1
            if capson=1 then keypressed99$="X" else keypressed99$="x"
        endif
        if getrawkeyreleased(67)=1
            if capson=1 then keypressed99$="C" else keypressed99$="c"
        endif
        if getrawkeyreleased(86)=1
            if capson=1 then keypressed99$="V" else keypressed99$="v"
        endif
        if getrawkeyreleased(66)=1
            if capson=1 then keypressed99$="B" else keypressed99$="b"
        endif
        if getrawkeyreleased(78)=1
            if capson=1 then keypressed99$="N" else keypressed99$="n"
        endif
        if getrawkeyreleased(77)=1
            if capson=1 then keypressed99$="M" else keypressed99$="m"
        endif
//new
		if getrawkeyreleased(188)=1
            keypressed99$=","
        endif
		if getrawkeyreleased(190)=1
            keypressed99$="."
        endif
        //spacekey
        if getrawkeyreleased(32)=1 then keypressed99$=" "


        //is it a key of interest?
        if keypressed99$<>""
            vv=len(mytextstring$)
            if vv<chmax
                mytextstring$=mytextstring$+keypressed99$
            endif
            keypressed99$=""
            //fix it
            settextstring(actualstring,mytextstring$)
            vv=len(mytextstring$)
            //settextstring(actualstring,mytextstring$)
            mkstr$=""
            for y=1 to vv step 1
                mkstr$=mkstr$+" "
            next y
            mkstr$=mkstr$+"|"
            settextstring(flash,mkstr$)
        endif



    endif


    //does a mouse exist?
    //allow for click input
    if getmouseexists()=1
        //print(getrawmousex()-drawx1)
        //print(getrawmousey()-drawy1)
        mmxx1#=getrawmousex()-drawx1
        mmyy1#=getrawmousey()-drawy1

        if getrawmouseleftreleased()=1
            keypressed99$=""
            if mmxx1#>30 and mmxx1#<81 and mmyy1#>74 and mmyy1#<114
                //escapekey
                drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        
                        sync()
                    endwhile
                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    exitfunction original$
                //exit function canceled
            endif

            if mmxx1#>95 and mmxx1#<148 and mmyy1#>74 and mmyy1#<114 then keypressed99$="1"
            if mmxx1#>160 and mmxx1#<212 and mmyy1#>74 and mmyy1#<114 then keypressed99$="2"
            if mmxx1#>224 and mmxx1#<277 and mmyy1#>74 and mmyy1#<114 then keypressed99$="3"
            if mmxx1#>289 and mmxx1#<340 and mmyy1#>74 and mmyy1#<114 then keypressed99$="4"
            if mmxx1#>353 and mmxx1#<406 and mmyy1#>74 and mmyy1#<114 then keypressed99$="5"
            if mmxx1#>418 and mmxx1#<470 and mmyy1#>74 and mmyy1#<114 then keypressed99$="6"
            if mmxx1#>483 and mmxx1#<535 and mmyy1#>74 and mmyy1#<114 then keypressed99$="7"
            if mmxx1#>548 and mmxx1#<600 and mmyy1#>74 and mmyy1#<114 then keypressed99$="8"
            if mmxx1#>612 and mmxx1#<663 and mmyy1#>74 and mmyy1#<114 then keypressed99$="9"
            if mmxx1#>676 and mmxx1#<727 and mmyy1#>74 and mmyy1#<114 then keypressed99$="0"
            //if mmxx1#>739 and mmxx1#<792 and mmyy1#>74 and mmyy1#<114 then keypressed99$="_"
			if capson=1 and mmxx1#>739 and mmxx1#<792 and mmyy1#>74 and mmyy1#<114 then keypressed99$="_"
			if capson=0 and mmxx1#>739 and mmxx1#<792 and mmyy1#>74 and mmyy1#<114 then keypressed99$="-"
			
			if mmxx1#>628 and mmxx1#<691 and mmyy1#>217 and mmyy1#<255 then keypressed99$=","
			if mmxx1#>691 and mmxx1#<744 and mmyy1#>217 and mmyy1#<255 then keypressed99$="."

            if mmxx1#>869 and mmxx1#<984 and mmyy1#>74 and mmyy1#<114
                //delete key
                 //lets delete a key
                    vv=len(mytextstring$)
                    if vv=1
                        mytextstring$=""
                    endif
                    if vv>1
                        newstring$=left(mytextstring$,vv-1)
                        mytextstring$=newstring$
                    endif
                myflash=31
                flashit=1
            endif

            if mmxx1#>31 and mmxx1#<132 and mmyy1#>168 and mmyy1#<208
                rp=adjustalpha(capson,usetext1)
                capson=rp
                myflash=31
                flashit=1
            endif

            //spacekey
            if mmxx1#>288 and mmxx1#<599 and mmyy1#>261 and mmyy1#<301 then keypressed99$=" "
            //enterkey1
            if mmxx1#>901 and mmxx1#<986 and mmyy1#>122 and mmyy1#<207 then keypressed99$="ENTER"
            //enterkey2
            if mmxx1#>853 and mmxx1#<986 and mmyy1#>170 and mmyy1#<207 then keypressed99$="ENTER"

            if keypressed99$="ENTER"
                 //delete resources
                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        sync()
                    endwhile
                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    if mytextstring$="" then mytextstring$="null"
                    exitfunction mytextstring$
                    //exit function return string

                keypressed99$=""
            endif


            if capson=1
                if mmxx1#>127 and mmxx1#<180 and mmyy1#>122 and mmyy1#<160 then keypressed99$="Q"
                if mmxx1#>193 and mmxx1#<244 and mmyy1#>122 and mmyy1#<160 then keypressed99$="W"
                if mmxx1#>254 and mmxx1#<309 and mmyy1#>122 and mmyy1#<160 then keypressed99$="E"
                if mmxx1#>319 and mmxx1#<372 and mmyy1#>122 and mmyy1#<160 then keypressed99$="R"
                if mmxx1#>385 and mmxx1#<438 and mmyy1#>122 and mmyy1#<160 then keypressed99$="T"
                if mmxx1#>450 and mmxx1#<502 and mmyy1#>122 and mmyy1#<160 then keypressed99$="Y"
                if mmxx1#>515 and mmxx1#<568 and mmyy1#>122 and mmyy1#<160 then keypressed99$="U"
                if mmxx1#>580 and mmxx1#<630 and mmyy1#>122 and mmyy1#<160 then keypressed99$="I"
                if mmxx1#>644 and mmxx1#<696 and mmyy1#>122 and mmyy1#<160 then keypressed99$="O"
                if mmxx1#>710 and mmxx1#<760 and mmyy1#>122 and mmyy1#<160 then keypressed99$="P"

                if mmxx1#>143 and mmxx1#<196 and mmyy1#>168 and mmyy1#<208 then keypressed99$="A"
                if mmxx1#>207 and mmxx1#<259 and mmyy1#>168 and mmyy1#<208 then keypressed99$="S"
                if mmxx1#>273 and mmxx1#<325 and mmyy1#>168 and mmyy1#<208 then keypressed99$="D"
                if mmxx1#>338 and mmxx1#<390 and mmyy1#>168 and mmyy1#<208 then keypressed99$="F"
                if mmxx1#>401 and mmxx1#<454 and mmyy1#>168 and mmyy1#<208 then keypressed99$="G"
                if mmxx1#>466 and mmxx1#<517 and mmyy1#>168 and mmyy1#<208 then keypressed99$="H"
                if mmxx1#>532 and mmxx1#<581 and mmyy1#>168 and mmyy1#<208 then keypressed99$="J"
                if mmxx1#>595 and mmxx1#<646 and mmyy1#>168 and mmyy1#<208 then keypressed99$="K"
                if mmxx1#>659 and mmxx1#<712 and mmyy1#>168 and mmyy1#<208 then keypressed99$="L"

                if mmxx1#>175 and mmxx1#<227 and mmyy1#>216 and mmyy1#<255 then keypressed99$="Z"
                if mmxx1#>240 and mmxx1#<293 and mmyy1#>216 and mmyy1#<255 then keypressed99$="X"
                if mmxx1#>303 and mmxx1#<356 and mmyy1#>216 and mmyy1#<255 then keypressed99$="C"
                if mmxx1#>370 and mmxx1#<421 and mmyy1#>216 and mmyy1#<255 then keypressed99$="V"
                if mmxx1#>433 and mmxx1#<486 and mmyy1#>216 and mmyy1#<255 then keypressed99$="B"
                if mmxx1#>498 and mmxx1#<550 and mmyy1#>216 and mmyy1#<255 then keypressed99$="N"
                if mmxx1#>562 and mmxx1#<615 and mmyy1#>216 and mmyy1#<255 then keypressed99$="M"


            endif



            if capson=0
                if mmxx1#>127 and mmxx1#<180 and mmyy1#>122 and mmyy1#<160 then keypressed99$="q"
                if mmxx1#>193 and mmxx1#<244 and mmyy1#>122 and mmyy1#<160 then keypressed99$="w"
                if mmxx1#>254 and mmxx1#<309 and mmyy1#>122 and mmyy1#<160 then keypressed99$="e"
                if mmxx1#>319 and mmxx1#<372 and mmyy1#>122 and mmyy1#<160 then keypressed99$="r"
                if mmxx1#>385 and mmxx1#<438 and mmyy1#>122 and mmyy1#<160 then keypressed99$="t"
                if mmxx1#>450 and mmxx1#<502 and mmyy1#>122 and mmyy1#<160 then keypressed99$="y"
                if mmxx1#>515 and mmxx1#<568 and mmyy1#>122 and mmyy1#<160 then keypressed99$="u"
                if mmxx1#>580 and mmxx1#<630 and mmyy1#>122 and mmyy1#<160 then keypressed99$="i"
                if mmxx1#>644 and mmxx1#<696 and mmyy1#>122 and mmyy1#<160 then keypressed99$="o"
                if mmxx1#>710 and mmxx1#<760 and mmyy1#>122 and mmyy1#<160 then keypressed99$="p"

                if mmxx1#>143 and mmxx1#<196 and mmyy1#>168 and mmyy1#<208 then keypressed99$="a"
                if mmxx1#>207 and mmxx1#<259 and mmyy1#>168 and mmyy1#<208 then keypressed99$="s"
                if mmxx1#>273 and mmxx1#<325 and mmyy1#>168 and mmyy1#<208 then keypressed99$="d"
                if mmxx1#>338 and mmxx1#<390 and mmyy1#>168 and mmyy1#<208 then keypressed99$="f"
                if mmxx1#>401 and mmxx1#<454 and mmyy1#>168 and mmyy1#<208 then keypressed99$="g"
                if mmxx1#>466 and mmxx1#<517 and mmyy1#>168 and mmyy1#<208 then keypressed99$="h"
                if mmxx1#>532 and mmxx1#<581 and mmyy1#>168 and mmyy1#<208 then keypressed99$="j"
                if mmxx1#>595 and mmxx1#<646 and mmyy1#>168 and mmyy1#<208 then keypressed99$="k"
                if mmxx1#>659 and mmxx1#<712 and mmyy1#>168 and mmyy1#<208 then keypressed99$="l"

                if mmxx1#>175 and mmxx1#<227 and mmyy1#>216 and mmyy1#<255 then keypressed99$="z"
                if mmxx1#>240 and mmxx1#<293 and mmyy1#>216 and mmyy1#<255 then keypressed99$="x"
                if mmxx1#>303 and mmxx1#<356 and mmyy1#>216 and mmyy1#<255 then keypressed99$="c"
                if mmxx1#>370 and mmxx1#<421 and mmyy1#>216 and mmyy1#<255 then keypressed99$="v"
                if mmxx1#>433 and mmxx1#<486 and mmyy1#>216 and mmyy1#<255 then keypressed99$="b"
                if mmxx1#>498 and mmxx1#<550 and mmyy1#>216 and mmyy1#<255 then keypressed99$="n"
                if mmxx1#>562 and mmxx1#<615 and mmyy1#>216 and mmyy1#<255 then keypressed99$="m"
            endif
        endif


        //is it a key of interest?
        if keypressed99$<>""
            vv=len(mytextstring$)
            if vv<chmax
                mytextstring$=mytextstring$+keypressed99$
            endif
            keypressed99$=""
            //fix it
            settextstring(actualstring,mytextstring$)
            vv=len(mytextstring$)
            //settextstring(actualstring,mytextstring$)
            mkstr$=""
            for y=1 to vv step 1
                mkstr$=mkstr$+" "
            next y
            mkstr$=mkstr$+"|"
            settextstring(flash,mkstr$)
        endif
    endif




   //does a touch screen exist
    //allow for click input
    if getmultitouchexists()=1
        tid=GetRawTouchCount(1)
        if tid>0
            x=getrawfirsttouchevent(1)
            for t1=1 to tid step 1
                sx1#=GetRawTouchStartX(x)
                sy1#=GetRawTouchStarty(x)
                x1#=GetRawTouchCurrentX(x)
                y1#=GetRawTouchCurrenty(x)
                if getrawtouchreleased(x)=1


                //print(getrawmousex()-drawx1)
                //print(getrawmousey()-drawy1)
                mmxx1#=((sx1#-drawx1)+(x1#-drawx1))/2.0
                mmyy1#=((sy1#-drawy1)+(y1#-drawy1))/2.0

            keypressed99$=""
            if mmxx1#>30 and mmxx1#<81 and mmyy1#>74 and mmyy1#<114
                //escapekey
                drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        
                        //animations if present
						for t=1 to 21 step 1
							updateobjectanimation(t)
							//if getobjectexists(t)=1
							//moveobjectz(t,1)
							//endif
						next t
                        
                        
                        
                        sync()
                    endwhile
                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    exitfunction original$
                //exit function canceled
            endif

            if mmxx1#>95 and mmxx1#<148 and mmyy1#>74 and mmyy1#<114 then keypressed99$="1"
            if mmxx1#>160 and mmxx1#<212 and mmyy1#>74 and mmyy1#<114 then keypressed99$="2"
            if mmxx1#>224 and mmxx1#<277 and mmyy1#>74 and mmyy1#<114 then keypressed99$="3"
            if mmxx1#>289 and mmxx1#<340 and mmyy1#>74 and mmyy1#<114 then keypressed99$="4"
            if mmxx1#>353 and mmxx1#<406 and mmyy1#>74 and mmyy1#<114 then keypressed99$="5"
            if mmxx1#>418 and mmxx1#<470 and mmyy1#>74 and mmyy1#<114 then keypressed99$="6"
            if mmxx1#>483 and mmxx1#<535 and mmyy1#>74 and mmyy1#<114 then keypressed99$="7"
            if mmxx1#>548 and mmxx1#<600 and mmyy1#>74 and mmyy1#<114 then keypressed99$="8"
            if mmxx1#>612 and mmxx1#<663 and mmyy1#>74 and mmyy1#<114 then keypressed99$="9"
            if mmxx1#>676 and mmxx1#<727 and mmyy1#>74 and mmyy1#<114 then keypressed99$="0"
            if capson=1 and mmxx1#>739 and mmxx1#<792 and mmyy1#>74 and mmyy1#<114 then keypressed99$="_"
			if capson=0 and mmxx1#>739 and mmxx1#<792 and mmyy1#>74 and mmyy1#<114 then keypressed99$="-"
			
			if mmxx1#>628 and mmxx1#<691 and mmyy1#>217 and mmyy1#<255 then keypressed99$=","
			if mmxx1#>691 and mmxx1#<744 and mmyy1#>217 and mmyy1#<255 then keypressed99$="."

            if mmxx1#>869 and mmxx1#<984 and mmyy1#>74 and mmyy1#<114
                //delete key
                 //lets delete a key
                    vv=len(mytextstring$)
                    if vv=1
                        mytextstring$=""
                    endif
                    if vv>1
                        newstring$=left(mytextstring$,vv-1)
                        mytextstring$=newstring$
                    endif
                myflash=31
                flashit=1
            endif

            if mmxx1#>31 and mmxx1#<132 and mmyy1#>168 and mmyy1#<208
                rp=adjustalpha(capson,usetext1)
                capson=rp
                myflash=31
                flashit=1
            endif

            //spacekey
            if mmxx1#>288 and mmxx1#<599 and mmyy1#>261 and mmyy1#<301 then keypressed99$=" "
            //enterkey1
            if mmxx1#>901 and mmxx1#<986 and mmyy1#>122 and mmyy1#<207 then keypressed99$="ENTER"
            //enterkey2
            if mmxx1#>853 and mmxx1#<986 and mmyy1#>170 and mmyy1#<207 then keypressed99$="ENTER"

            if keypressed99$="ENTER"
                 //delete resources
                    drawy1=scroll1
                    while getspritey(usesprite1)<scroll1
                        yy=getspritey(usesprite1)
                        setspriteposition(usesprite1,drawx1,yy+20)
                        for tt=usetext1 to maxresources step 1
                            if gettextexists(tt)=1
                                yy=gettexty(tt)
                                settextposition(tt,gettextx(tt),yy+20)
                            endif
                        next tt
                        sync()
                    endwhile
                    for tt=usetext1 to maxresources step 1
                        if gettextexists(tt)=1 then deletetext(tt)
                    next tt
                    deletesprite(usesprite1)
                    deleteimage(useimg1)
                    if mytextstring$="" then mytextstring$="null"
                    exitfunction mytextstring$
                    //exit function return string

                keypressed99$=""
            endif


            if capson=1
                if mmxx1#>127 and mmxx1#<180 and mmyy1#>122 and mmyy1#<160 then keypressed99$="Q"
                if mmxx1#>193 and mmxx1#<244 and mmyy1#>122 and mmyy1#<160 then keypressed99$="W"
                if mmxx1#>254 and mmxx1#<309 and mmyy1#>122 and mmyy1#<160 then keypressed99$="E"
                if mmxx1#>319 and mmxx1#<372 and mmyy1#>122 and mmyy1#<160 then keypressed99$="R"
                if mmxx1#>385 and mmxx1#<438 and mmyy1#>122 and mmyy1#<160 then keypressed99$="T"
                if mmxx1#>450 and mmxx1#<502 and mmyy1#>122 and mmyy1#<160 then keypressed99$="Y"
                if mmxx1#>515 and mmxx1#<568 and mmyy1#>122 and mmyy1#<160 then keypressed99$="U"
                if mmxx1#>580 and mmxx1#<630 and mmyy1#>122 and mmyy1#<160 then keypressed99$="I"
                if mmxx1#>644 and mmxx1#<696 and mmyy1#>122 and mmyy1#<160 then keypressed99$="O"
                if mmxx1#>710 and mmxx1#<760 and mmyy1#>122 and mmyy1#<160 then keypressed99$="P"

                if mmxx1#>143 and mmxx1#<196 and mmyy1#>168 and mmyy1#<208 then keypressed99$="A"
                if mmxx1#>207 and mmxx1#<259 and mmyy1#>168 and mmyy1#<208 then keypressed99$="S"
                if mmxx1#>273 and mmxx1#<325 and mmyy1#>168 and mmyy1#<208 then keypressed99$="D"
                if mmxx1#>338 and mmxx1#<390 and mmyy1#>168 and mmyy1#<208 then keypressed99$="F"
                if mmxx1#>401 and mmxx1#<454 and mmyy1#>168 and mmyy1#<208 then keypressed99$="G"
                if mmxx1#>466 and mmxx1#<517 and mmyy1#>168 and mmyy1#<208 then keypressed99$="H"
                if mmxx1#>532 and mmxx1#<581 and mmyy1#>168 and mmyy1#<208 then keypressed99$="J"
                if mmxx1#>595 and mmxx1#<646 and mmyy1#>168 and mmyy1#<208 then keypressed99$="K"
                if mmxx1#>659 and mmxx1#<712 and mmyy1#>168 and mmyy1#<208 then keypressed99$="L"

                if mmxx1#>175 and mmxx1#<227 and mmyy1#>216 and mmyy1#<255 then keypressed99$="Z"
                if mmxx1#>240 and mmxx1#<293 and mmyy1#>216 and mmyy1#<255 then keypressed99$="X"
                if mmxx1#>303 and mmxx1#<356 and mmyy1#>216 and mmyy1#<255 then keypressed99$="C"
                if mmxx1#>370 and mmxx1#<421 and mmyy1#>216 and mmyy1#<255 then keypressed99$="V"
                if mmxx1#>433 and mmxx1#<486 and mmyy1#>216 and mmyy1#<255 then keypressed99$="B"
                if mmxx1#>498 and mmxx1#<550 and mmyy1#>216 and mmyy1#<255 then keypressed99$="N"
                if mmxx1#>562 and mmxx1#<615 and mmyy1#>216 and mmyy1#<255 then keypressed99$="M"


            endif



            if capson=0
                if mmxx1#>127 and mmxx1#<180 and mmyy1#>122 and mmyy1#<160 then keypressed99$="q"
                if mmxx1#>193 and mmxx1#<244 and mmyy1#>122 and mmyy1#<160 then keypressed99$="w"
                if mmxx1#>254 and mmxx1#<309 and mmyy1#>122 and mmyy1#<160 then keypressed99$="e"
                if mmxx1#>319 and mmxx1#<372 and mmyy1#>122 and mmyy1#<160 then keypressed99$="r"
                if mmxx1#>385 and mmxx1#<438 and mmyy1#>122 and mmyy1#<160 then keypressed99$="t"
                if mmxx1#>450 and mmxx1#<502 and mmyy1#>122 and mmyy1#<160 then keypressed99$="y"
                if mmxx1#>515 and mmxx1#<568 and mmyy1#>122 and mmyy1#<160 then keypressed99$="u"
                if mmxx1#>580 and mmxx1#<630 and mmyy1#>122 and mmyy1#<160 then keypressed99$="i"
                if mmxx1#>644 and mmxx1#<696 and mmyy1#>122 and mmyy1#<160 then keypressed99$="o"
                if mmxx1#>710 and mmxx1#<760 and mmyy1#>122 and mmyy1#<160 then keypressed99$="p"

                if mmxx1#>143 and mmxx1#<196 and mmyy1#>168 and mmyy1#<208 then keypressed99$="a"
                if mmxx1#>207 and mmxx1#<259 and mmyy1#>168 and mmyy1#<208 then keypressed99$="s"
                if mmxx1#>273 and mmxx1#<325 and mmyy1#>168 and mmyy1#<208 then keypressed99$="d"
                if mmxx1#>338 and mmxx1#<390 and mmyy1#>168 and mmyy1#<208 then keypressed99$="f"
                if mmxx1#>401 and mmxx1#<454 and mmyy1#>168 and mmyy1#<208 then keypressed99$="g"
                if mmxx1#>466 and mmxx1#<517 and mmyy1#>168 and mmyy1#<208 then keypressed99$="h"
                if mmxx1#>532 and mmxx1#<581 and mmyy1#>168 and mmyy1#<208 then keypressed99$="j"
                if mmxx1#>595 and mmxx1#<646 and mmyy1#>168 and mmyy1#<208 then keypressed99$="k"
                if mmxx1#>659 and mmxx1#<712 and mmyy1#>168 and mmyy1#<208 then keypressed99$="l"

                if mmxx1#>175 and mmxx1#<227 and mmyy1#>216 and mmyy1#<255 then keypressed99$="z"
                if mmxx1#>240 and mmxx1#<293 and mmyy1#>216 and mmyy1#<255 then keypressed99$="x"
                if mmxx1#>303 and mmxx1#<356 and mmyy1#>216 and mmyy1#<255 then keypressed99$="c"
                if mmxx1#>370 and mmxx1#<421 and mmyy1#>216 and mmyy1#<255 then keypressed99$="v"
                if mmxx1#>433 and mmxx1#<486 and mmyy1#>216 and mmyy1#<255 then keypressed99$="b"
                if mmxx1#>498 and mmxx1#<550 and mmyy1#>216 and mmyy1#<255 then keypressed99$="n"
                if mmxx1#>562 and mmxx1#<615 and mmyy1#>216 and mmyy1#<255 then keypressed99$="m"
            endif



        //is it a key of interest?
        if keypressed99$<>""
            vv=len(mytextstring$)
            if vv<chmax
                mytextstring$=mytextstring$+keypressed99$
            endif
            keypressed99$=""
            //fix it
            settextstring(actualstring,mytextstring$)
            vv=len(mytextstring$)
            //settextstring(actualstring,mytextstring$)
            mkstr$=""
            for y=1 to vv step 1
                mkstr$=mkstr$+" "
            next y
            mkstr$=mkstr$+"|"
            settextstring(flash,mkstr$)
        endif



        endif
        x=getrawnexttouchevent()
        next t1

        endif
    endif




//animations if present
    for t=1 to 21 step 1
		updateobjectanimation(t)
		//if getobjectexists(t)=1
			//moveobjectz(t,1)
		//endif
	next t


 Sync()
loop

endfunction "null"

function grabkey$(ct,us,cp)
br$=""


if ct=us then br$="ESC"
if ct=us+1 then br$="1"
if ct=us+2 then br$="2"
if ct=us+3 then br$="3"
if ct=us+4 then br$="4"
if ct=us+5 then br$="5"
if ct=us+6 then br$="6"
if ct=us+7 then br$="7"
if ct=us+8 then br$="8"
if ct=us+9 then br$="9"
if ct=us+10 then br$="0"
//if ct=us+11 then br$="_"
//12=
if ct=us+13 then br$="DEL"

//enter
if ct=us+39 then br$="ENTER"
//caps
if ct=us+27 then br$="CAPS"

//space
if ct=us+55 then br$=" "


//is caps lock on
if cp=1
//row 2
if ct=us+11 then br$="_"
if ct=us+15 then br$="Q"
if ct=us+16 then br$="W"
if ct=us+17 then br$="E"
if ct=us+18 then br$="R"
if ct=us+19 then br$="T"
if ct=us+20 then br$="Y"
if ct=us+21 then br$="U"
if ct=us+22 then br$="I"
if ct=us+23 then br$="O"
if ct=us+24 then br$="P"

//row3

if ct=us+28 then br$="A"
if ct=us+29 then br$="S"
if ct=us+30 then br$="D"
if ct=us+31 then br$="F"
if ct=us+32 then br$="G"
if ct=us+33 then br$="H"
if ct=us+34 then br$="J"
if ct=us+35 then br$="K"
if ct=us+36 then br$="L"

if ct=us+41 then br$="Z"
if ct=us+42 then br$="X"
if ct=us+43 then br$="C"
if ct=us+44 then br$="V"
if ct=us+45 then br$="B"
if ct=us+46 then br$="N"
if ct=us+47 then br$="M"

endif


if cp=0
 //row 2
if ct=us+11 then br$="-"
if ct=us+15 then br$="q"
if ct=us+16 then br$="w"
if ct=us+17 then br$="e"
if ct=us+18 then br$="r"
if ct=us+19 then br$="t"
if ct=us+20 then br$="y"
if ct=us+21 then br$="u"
if ct=us+22 then br$="i"
if ct=us+23 then br$="o"
if ct=us+24 then br$="p"

//row3

if ct=us+28 then br$="a"
if ct=us+29 then br$="s"
if ct=us+30 then br$="d"
if ct=us+31 then br$="f"
if ct=us+32 then br$="g"
if ct=us+33 then br$="h"
if ct=us+34 then br$="j"
if ct=us+35 then br$="k"
if ct=us+36 then br$="l"

if ct=us+41 then br$="z"
if ct=us+42 then br$="x"
if ct=us+43 then br$="c"
if ct=us+44 then br$="v"
if ct=us+45 then br$="b"
if ct=us+46 then br$="n"
if ct=us+47 then br$="m"


endif

endfunction br$



function adjustalpha(cp,ct)
cp=cp+1
if cp=>2 then cp=0

if cp=1
settextstring(ct+11,"_")

settextstring(ct+15,"Q")
settextstring(ct+16,"W")
settextstring(ct+17,"E")
settextstring(ct+18,"R")
settextstring(ct+19,"T")
settextstring(ct+20,"Y")
settextstring(ct+21,"U")
settextstring(ct+22,"I")
settextstring(ct+23,"O")
settextstring(ct+24,"P")

settextstring(ct+28,"A")
settextstring(ct+29,"S")
settextstring(ct+30,"D")
settextstring(ct+31,"F")
settextstring(ct+32,"G")
settextstring(ct+33,"H")
settextstring(ct+34,"J")
settextstring(ct+35,"K")
settextstring(ct+36,"L")

settextstring(ct+41,"Z")
settextstring(ct+42,"X")
settextstring(ct+43,"C")
settextstring(ct+44,"V")
settextstring(ct+45,"B")
settextstring(ct+46,"N")
settextstring(ct+47,"M")


settextstring(ct+27,"Caps ON")

endif

if cp=0

settextstring(ct+11,"-")
//row 2
settextstring(ct+15,"q")
settextstring(ct+16,"w")
settextstring(ct+17,"e")
settextstring(ct+18,"r")
settextstring(ct+19,"t")
settextstring(ct+20,"y")
settextstring(ct+21,"u")
settextstring(ct+22,"i")
settextstring(ct+23,"o")
settextstring(ct+24,"p")

settextstring(ct+28,"a")
settextstring(ct+29,"s")
settextstring(ct+30,"d")
settextstring(ct+31,"f")
settextstring(ct+32,"g")
settextstring(ct+33,"h")
settextstring(ct+34,"j")
settextstring(ct+35,"k")
settextstring(ct+36,"l")

settextstring(ct+41,"z")
settextstring(ct+42,"x")
settextstring(ct+43,"c")
settextstring(ct+44,"v")
settextstring(ct+45,"b")
settextstring(ct+46,"n")
settextstring(ct+47,"m")



settextstring(ct+27,"CapsOFF")

endif

endfunction cp











function questionmessage(drawx,drawy,string$,spriteuse,imguse,txtuse,what)
setfolder("")

setfolder("media")
sync()
//local grabx#
//local graby#
if what=0 then loadimage(imguse,"data/questionbox.png") else loadimage(imguse,"data/messagebox.png")
createsprite(spriteuse,imguse)
setspriteposition(spriteuse,drawx,drawy)
setspritecolor(spriteuse,250,250,250,255)
setspritecoloralpha(spriteuse,255)
setspritedepth(spriteuse,1)
setspritevisible(spriteuse,1)	

createtext(txtuse,string$)
settextcolor(txtuse,50,50,50,255)
settextdepth(txtuse,1)
settextvisible(txtuse,1)
settextalignment(txtuse,1)
settextposition(txtuse,drawx+145,drawy+30)
settextsize(txtuse,20)
setfolder("")
do
	//print("x "+str(getrawmousex()-drawx))
	//print("y "+str(getrawmousey()-drawy))
	grabx=getrawmousex()-drawx
	graby=getrawmousey()-drawy
	if getrawmouseleftreleased()=1
		if grabx=>71 and grabx=<142 and graby=>90 and graby=<112 
			deletesprite(spriteuse)
			deletetext(txtuse)
			deleteimage(imguse)
			sync()
			exitfunction 1
		endif
		
		deletesprite(spriteuse)
		deletetext(txtuse)
		deleteimage(imguse)
		sync()
		exitfunction 0
	endif
	
	sync()
loop
	
endfunction 0





function messagebox(drawx,drawy,string1$,string2$,string3$,string4$,txtsize,spriteuse)
setfolder("")

setfolder("media")
sync()
what=1
imguse=spriteuse
txtuse=spriteuse
//local grabx#
//local graby#
if what=0 then loadimage(imguse,"data/questionbox.png") else loadimage(imguse,"data/messagebox.png")
createsprite(spriteuse,imguse)
setspriteposition(spriteuse,drawx,drawy)
setspritecolor(spriteuse,250,250,250,255)
setspritecoloralpha(spriteuse,255)
setspritedepth(spriteuse,1)
setspritevisible(spriteuse,1)	

createtext(txtuse,string1$)
settextcolor(txtuse,20,20,20,255)
settextdepth(txtuse,1)
settextvisible(txtuse,1)
settextalignment(txtuse,1)
settextposition(txtuse,drawx+145,drawy+5)
settextsize(txtuse,20)

createtext(txtuse+1,string2$)
settextcolor(txtuse+1,40,40,40,255)
settextdepth(txtuse+1,1)
settextvisible(txtuse+1,1)
settextalignment(txtuse+1,1)
settextposition(txtuse+1,drawx+145,drawy+30)
settextsize(txtuse+1,txtsize)

createtext(txtuse+2,string3$)
settextcolor(txtuse+2,40,40,40,255)
settextdepth(txtuse+2,1)
settextvisible(txtuse+2,1)
settextalignment(txtuse+2,1)
settextposition(txtuse+2,drawx+145,drawy+45)
settextsize(txtuse+2,txtsize)

createtext(txtuse+3,string4$)
settextcolor(txtuse+3,40,40,40,255)
settextdepth(txtuse+3,1)
settextvisible(txtuse+3,1)
settextalignment(txtuse+3,1)
settextposition(txtuse+3,drawx+145,drawy+60)
settextsize(txtuse+3,txtsize)


setfolder("")
do
	//print("x "+str(getrawmousex()-drawx))
	//print("y "+str(getrawmousey()-drawy))
	grabx=getrawmousex()-drawx
	graby=getrawmousey()-drawy
	if getrawmouseleftreleased()=1
		if grabx=>71 and grabx=<142 and graby=>90 and graby=<112 
			deletesprite(spriteuse)
			deletetext(txtuse)
			deletetext(txtuse+1)
			deletetext(txtuse+2)
			deletetext(txtuse+3)
			deleteimage(imguse)
			sync()
			exitfunction 1
		endif
		
		deletesprite(spriteuse)
		deletetext(txtuse)
		deletetext(txtuse+1)
		deletetext(txtuse+2)
		deletetext(txtuse+3)
		deleteimage(imguse)
		sync()
		exitfunction 0
	endif
	
	sync()
loop
	
endfunction 0





function updatefilelist(liststart,sbox,ebox)
setfolder("")
dd=liststart
if dd<0 then dd=0
for t=sbox to ebox step 1
    //message(str(dd))
    settextstring(t,file$[dd])
    if file$[dd]="" then settextstring(t,".........")
    dd=dd+1
next t
endfunction


function functionfile$(drawx,drawy,spriteuse,imguse,textuse,currentfile$,ext$,title$,mymode)
global maxfiles99
local maximumfiles
local mf
local ex$
local yy
local scroll=0
ex$=ext$
sync()
//change this if you need more files
filesmax99=1000
scroll=0
mf=filesmax99
dim file$[1000]
prioritysf=2
//mymode 0 save
//mymode 1 load


//dump old file$
for t=0 to filesmax99 step 1
    file$[t]=""
next t

//populate new
//refresh list
//b=SetCurrentDir(".")

//SetRawWritePath( GetDocumentsPath())
//ok=questionmessage(50,50,getcurrentdir(),1000,1000,1000,1)
//ok=questionmessage(50,50,d$,15000,15000,15000,0)
//setcurrentdir(d$)
//MakeFolder( "AGK 3D Animations")
//setcurrentdir("AGK 3D Animations")
//dd$=chooserawfile(".ob2")
//doesnt work
//MakeFolder( d$+"\AGK 3D Animations")
//end
setfolder("")
a$=getfirstfile()
file$[0]=""
x=0
if right(a$,3)=ext$
    file$[x]=a$
            x=x+1
        endif
        for c=1 to filesmax99 step 1
            file$[c]=""
            a$=getnextfile()
            if a$<>"" and x<1000 and right(a$,3)=ext$
                file$[x]=a$
                x=x+1
            endif
        next c
maximumfiles=x-1
//message( str(x-1))
//test random data

startresources=textuse
//look and see if that file exists in list
active=textuse+1
if currentfile$<>""
found=0
for t=0 to filesmax99 step 1
    if file$[t]=currentfile$
        found=1
        scroll=t-8
        if scroll<0 then scroll=0
    endif
next t
if found=0 and mymode=1 then currentfile$=""
endif

if scroll>maximumfiles then scroll=maximumfiles
if scroll<0 then scroll=0

if scroll=>maxfiles99-8 then scroll=maxfiles99-8

//look threw list and see whats active
setfolder("media")

loadimage(imguse,"data/fileexplorer.png")
createsprite(spriteuse,imguse)
setspriteposition(spriteuse,drawx,drawy)
setspritecolor(spriteuse,250,250,250,255)
setspritecoloralpha(spriteuse,240)
setspritedepth(spriteuse,prioritysf)

loadimage(imguse+1,"data/delfile.png")
createsprite(spriteuse+1,imguse+1)
setspriteposition(spriteuse+1,drawx+150,drawy+75)
setspritecolor(spriteuse+1,250,250,250,255)
setspritecoloralpha(spriteuse+1,255)
setspritedepth(spriteuse+1,1)
setspritevisible(spriteuse+1,0)


txt=textuse

createtext(txt,"")
settextstring(txt,title$)
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+7)
SetTextAlignment(txt,1 )
settextsize(txt,30)
settextdepth(txt,prioritysf)

txt=txt+1
startbox=txt
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+45)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)


txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+65)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+85)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+105)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+125)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+145)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+165)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+185)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)

txt=txt+1
createtext(txt,"This is my first file.dat")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+300,drawy+205)
SetTextAlignment(txt,1 )
settextsize(txt,20)
settextdepth(txt,prioritysf)
endbox=txt

txt=txt+1
createtext(txt,currentfile$)
settextcolor(txt,255,0,0,255)
settextposition(txt,drawx+135,drawy+242)
SetTextAlignment(txt,1 )
settextsize(txt,22)
settextdepth(txt,prioritysf)
entrybox=txt

txt=txt+1
if mymode=0 then createtext(txt,"SAVE") else createtext(txt,"LOAD")

settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+338,drawy+242)
SetTextAlignment(txt,1 )
settextsize(txt,25)
settextdepth(txt,prioritysf)
loadbutton=txt

txt=txt+1
createtext(txt,"DEL")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+430,drawy+242)
SetTextAlignment(txt,1 )
settextsize(txt,25)
settextdepth(txt,prioritysf)
delbutton=txt

txt=txt+1
createtext(txt,"ESC")
settextcolor(txt,20,20,20,255)
settextposition(txt,drawx+522,drawy+242)
SetTextAlignment(txt,1 )
settextsize(txt,25)
settextdepth(txt,prioritysf)
cancelbutton=txt

endresources=cancelbutton

updatefilelist(scroll,startbox,endbox)

//set active file if its in the list
if currentfile$<>""
a=-1
if scroll<0 then scroll=0



for t=scroll to scroll+8 step 1
    a=a+1
    if file$[t]=currentfile$ and currentfile$<>""
        //message("found")
        active=startbox+a
    endif
next t
endif


//active=startbox
menu=1
sel=0

if mymode=1 and found=0
menu=0

endif

//hack
scroll=0
slidex2#=0

flash=0
myflash=0
//active=startbox
do
    myflash=myflash+1
    if myflash=>30
        myflash=0
        flash=flash+1
        if flash=>2 then flash=0

        if flash=1
            if menu=0 then settextcolor(active,255,0,0,255)
            if menu=1 then settextcolor(active,0,255,0,255)
            if sel=1 and menu=1 then settextcolor(loadbutton,255,0,0,255)
            if sel=2 and menu=1 then settextcolor(delbutton,255,0,0,255)
            if sel=3 and menu=1 then settextcolor(cancelbutton,255,0,0,255)
            if sel=0 and menu=1 then settextcolor(entrybox,255,0,0,255)
        endif

        if flash=0
            settextcolor(active,20,20,20,255)
            if menu=1 then settextcolor(active,0,255,0,255)
            if sel=1 and menu=1 then settextcolor(loadbutton,20,20,20,255)
            if sel=2 and menu=1 then settextcolor(delbutton,20,20,20,255)
            if sel=3 and menu=1 then settextcolor(cancelbutton,20,20,20,255)
            if sel=0 and menu=1 then settextcolor(entrybox,20,20,20,255)
        endif
    endif



    //check for mouse input
    //for t=1 to 4 step 1
        if getmouseexists()=1

            //navigation
            //xx#=getrawmousewheel()
            yy=0
			if lastwheel#<getrawmousewheel()
				yy=10
				lastwheel#=getrawmousewheel()
			endif
			if lastwheel#>getrawmousewheel()
				yy=-10
				lastwheel#=getrawmousewheel()
			endif
			//print("x "+str(getrawmousex()-drawx))
			//print("y "+str(getrawmousey()-drawy))
			
			//print(yy#)
            //slidey2#=yy#
            //slidex2#=slidex2#+xx#

			grabx#=getrawmousex()-drawx
			graby#=getrawmousey()-drawy
			if getrawmouseleftreleased()=1
				if grabx#=>15 and grabx#=<257 and graby#=>238 and graby#=<270 and mymode=0
					//load keyboard entry
					newstring$=joystickkeyboard$(0,200,0,12,10000,10000,10000,gettextstring(entrybox))
					currentfile$=newstring$
					if right(currentfile$,4)<>".ob2" then currentfile$=currentfile$+".ob2"
					settextstring(entrybox,currentfile$)
					myflash=31
					flash=0
					active=startbox
					updatefilelist(scroll,startbox,endbox)
				endif
				
				
				if grabx#=>20 and grabx#=<540
						if graby#=>45 and graby#=<64
							//file 1
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox
							updatefilelist(scroll,startbox,endbox)
						
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
						
						endif
						if graby#=>65 and graby#=<84
							//file 2
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+1
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>85 and graby#=<104
							//file 3
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+2
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>105 and graby#=<124
							//file 4
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+3
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>125 and graby#=<144
							//file 5
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+4
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>145 and graby#=<164
							//file 6
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+5
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>165 and graby#=<184
							//file 7
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+6
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>185 and graby#=<204
							//file 8
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+7
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						if graby#=>205 and graby#=<224
							//file 9
							settextcolor(active,20,20,20,255)
							myflash=31
							flash=0
							active=startbox+8
							updatefilelist(scroll,startbox,endbox)
							
							if scroll=<0 then scroll=0
							if currentfile$<>file$[active-startbox+scroll]
								if file$[active-startbox+scroll]<>"" then currentfile$=file$[active-startbox+scroll]
								settextstring(entrybox,currentfile$)
								sel=0
								//menu=1
								settextcolor(entrybox,0,255,0,255)
								if file$[active-startbox+scroll]<>"" then sel=1 else sel=0
							endif
							
						endif
						
				//end 9 file selection
				endif
				
				
				if graby#=>240 and graby#=<270
						if grabx#=>300 and grabx#=<375
							//load the file
							//save the file or loadit
							if currentfile$<>""
								//if getfileexists(file$[active-startbox+scroll])=1
								//deletefile(file$[active-startbox+scroll])
								//delete resources
								entry$=gettextstring(entrybox)
								for s=startresources to endresources+1
									if gettextexists(s)=1 then deletetext(s)
								next s
								deletesprite(spriteuse)
								deleteimage(imguse)
								deletesprite(spriteuse+1)
								deleteimage(imguse+1)
								mystring$=currentfile$
								exitfunction mystring$
								//if mymode=0 then exitfunction entry$
							endif	
						endif
					
						if grabx#=>390 and grabx#=<465
							//delete file
							if currentfile$<>""
								setspritevisible(spriteuse+1,1)
								question=0
								while question=0
									//print(getrawmousex()-drawx)
									//print(getrawmousey()-drawy)
									grabx#=getrawmousex()-drawx
									graby#=getrawmousey()-drawy
									if getrawmouseleftreleased()=1
										if grabx#=>300 and grabx#=<373 and graby#=>166 and graby#=<187
											question=2
										endif
										if grabx#=>220 and grabx#=<294 and graby#=>166 and graby#=<187
											question=1
										endif
									endif
									sync()
								endwhile
								setspritevisible(spriteuse+1,0)
								
								//ok what was selected?
								//1 delete
								//2 no cancel
								if question=1
									scroll=0
									if getfileexists(currentfile$)=1 then deletefile(currentfile$)
									settextstring(entrybox,"")
									currentfile$=""
									refreshmyfilelist(ex$,mf)
									updatefilelist(scroll,startbox,endbox)

									//set active file if its in the list
									if currentfile$<>""
										a=-1
										if scroll<0 then scroll=0
										for t=scroll to scroll+8 step 1
											a=a+1
											if file$[t]=currentfile$
												//message("found")
												active=startbox+a
											endif
										next t
									endif
									
									
								endif
								
							endif
						endif
					
						if grabx#=>485 and grabx#=<560
							//cancel
							//if getfileexists(file$[active-startbox+scroll])=1

                            //delete resources
                            for s=startresources to endresources+1
                                if gettextexists(s)=1 then deletetext(s)
                            next s
                            deletesprite(spriteuse)
                            deleteimage(imguse)
                            deletesprite(spriteuse+1)
							deleteimage(imguse+1)
                            exitfunction "null"	
						endif
					
				//end load del esc/ file input
				endif
				
				//end mouseleftclick
			endif

                if yy=>10
                    yy=0
                    settextcolor(active,20,20,20,255)
                    active=active-1
                    myflash=31
                    flash=0
                    if active<startbox
                        active=startbox

                        //do scroll function here
                        scroll=scroll-1
                        if scroll<0 then scroll=0
                        updatefilelist(scroll,startbox,endbox)
                    endif

                endif

                if yy=<-10
                    yy=0
                    settextcolor(active,20,20,20,255)
                    active=active+1
                    myflash=31
                    flash=0

                    if active>endbox
                        active=endbox

                        //do scroll here
                        scroll=scroll+1
                        if scroll>maximumfiles-8
                            scroll=maximumfiles-8
                            if scroll<0 then scroll=0
                        endif

                        updatefilelist(scroll,startbox,endbox)
                    endif
                endif
            //endif
        endif














    sync()
loop

endfunction "null"







function refreshmyfilelist(ex$,mf)
setfolder("")
//dump old file$
for t=0 to mf step 1
    file$[t]=""
next t
//populate new
//refresh list
//b=SetCurrentDir(".")

//b=SetCurrentDir(".")
//d$=GetDocumentsPath()
//setcurrentdir(d$)
//MakeFolder( "AGK 3D Animations")
//setcurrentdir(d$+"/AGK 3D Animations")


a$=getfirstfile()
file$[0]=""
x=0
if right(a$,3)=ex$
    file$[x]=a$
    x=x+1
endif
        for c=1 to mf step 1
            file$[c]=""
            a$=getnextfile()
            if a$<>"" and x<1000 and right(a$,3)=ex$
                file$[x]=a$
                x=x+1
            endif
        next c

endfunction






















