User Tools

Site Tools


mmbasic:sinput_string_input_with_edit_functions

sinput - String input, with edit functions

'
' Program: sinput.bas
'
' String input, with edit functions
'
' Date: 2017.09.10 Peter Svard
'

' make a index memory of 5 strings
Dim m$(5) As string
m$(1)="One One One One One 1 1 1 1 1"
m$(2)="Two Two Two Two Two 2 2 2 2 2"
m$(3)="Three Three Three Three Three 3 3 3 3 3"
m$(4)="Four Four Four Four Four 4 4 4 4 4"
m$(5)="Five Five Five Five Five 5 5 5 5 5"


' print some text on screen for this test
Cls
Print "Testing sinput"
Print
Cursor 20,10:Print "0123456789012345678901234567890123456789"

' --- test sinput
Dim xs As string
Dim p As integer
Dim ekey As integer
Dim c As integer
p=10 ' set cursor position
c=1 ' index counter
Do
For i=2 To 6:Cursor 20,i:Print Space$(50):Next i
' show all five index strings to screen
Cursor 20,2:Print m$(1)
Cursor 20,3:Print m$(2)
Cursor 20,4:Print m$(3)
Cursor 20,5:Print m$(4)
Cursor 20,6:Print m$(5)
xs=m$(c)
sinput xs,20,12,p,40,ekey ' edit string
m$(c)=xs ' save string to index
If ekey=128 And c>1 Then c=c-1 ' cursor up
If ekey=129 And c<5 Then c=c+1 ' cursor down
'Cursor 10,3:Print xs
Cursor 20,15:Print"eKey-code = ";ekey;"  "
Loop

End

'
' sinput
'
' Variables:
'      xs  string
' col,row  screen position
'       p  cursor position in string (-1=auto pos.)
'     max  max characters in string
'    ekey  return key-code (13=Enter, Up/Down, PgUp/PgDn, F1-F10, Esc)
'
Sub sinput(xs As string,col As integer,row As integer,p As integer,max As integer, ekey As integer)
Local s As string
s = xs
' check if string is to long
If Len(s)>max Then s=Mid$(s,1,max) ' cut of string to max-value
' check if string is to short, set to max-value
If Len(s)<max Then s=s+Space$(max-Len(s)) ' extend to max
' local variables
Local t As integer ' inkey$ key-code
Local w As integer ' last character in string
' init rutin before main-loop
ekey=0 ' eKey reset
Cursor col,row ' position cursor
Print s ' print out string
If p=-1 Then p=Len(s) ' set cursor auto position
' start main loop for sinput
Do
Cursor col+p,row
t=Asc(Inkey$) ' wait for a key
If t=8 And p>0 Then ' backspace
    s=Mid$(s,1,p-1)+Mid$(s,p+1)+Chr$(32)
    p=p-1
End If
If t=127 Then ' Delete
    s=Mid$(s,1,p)+Mid$(s,p+2)+Chr$(32)
End If
If t=130 And p>0 Then p=p-1 ' cursor left
If t=131 And p<max Then p=p+1 ' cursor right
' some eKey code
If t=27 Or t=13 Then ekey=t:Exit Do ' Esc or Enter
If t=128 Or t=129 Then ekey=t:Exit Do ' cursor Up or Down
If t=136 Or t=137 Then ekey=t:Exit Do ' PgUp or PgDn
If t>144 And t<155 Then ekey=t:Exit Do ' F1-F10
If (t>31 And t<127) Then ' ASCII 32-126 characters
    w=Asc(Mid$(s,max,1)) ' last character
    If p=0 Then
        If w=32 Then s=Chr$(t)+Mid$(s,p+1) ' first position
        Else
        If w=32 Then s=Mid$(s,1,p)+Chr$(t)+Mid$(s,p+1) ' somewhere else position
    End If
    If p<max And w=32 Then p=p+1 ' move cursor to right
End If

If t<>0 Then ' update string to screen
    Cursor col,row:Print Space$(max)
    Cursor col,row:Print s
End If

Loop
xs=s
End Sub
mmbasic/sinput_string_input_with_edit_functions.txt · Last modified: 2024/01/29 15:55 by gerry