First app in SB. Text input line.

This is a bare bones line input demo routine. Parameters can be added to move it to different columns and rows. It includes a cursor routine and can toggle between insert and overwrite by pressing the ` key. It is limited to one line of text input. Currently the Enter key just prints the input [word] to the screen and quits. It can be easily adapted to use the input [word] in a program.

Thanks to Mark and easylangs for answering my questions about using SB vs QB/QB64 and keeping me engaged in the project. It was fun. Now all it needs is a sleep function.... for me. I'm getting to old for all-nighters.

- Pete

Key Input Routine Assigned Keys and Actions:

Tab = Delete.
Arrow Left = Cursor left.
Arrow Right = Cursor right.
Arrow Up = Cursor home.
Arrow Down = Cursor end.
` = Toggle Insert/Overwrite modes. (Cursor changes shape.)

==================================================

Const ESCS = Chr(27) + "[" ' Escape sequence prefix.
Const LINE_ON  = ESCS + "4m"  ' underline
Const LINE_OFF = ESCS + "24m"
Def uline(text) = LINE_ON + text + LINE_OFF
toggleblink = 1
inscur = "_"
word = ""
mykey = ""
z = -1
PRINT "Input: ";
startpos = XPOS
endpos = 48
WHILE 1
  mykey = INKEY
  IF LEN(mykey) THEN
    IF XPOS - startpos < LEN(word) THEN
      PRINT MID(word, XPOS - startpos + 1, 1);
      LOCATE YPOS, XPOS - 1
    ENDIF
    IF LEN(mykey) = 2 THEN
      SELECT CASE MID(mykey, 2, 1)
        CASE CHR(4) ' Cursor Left
          IF XPOS > startpos THEN
            IF XPOS - (startpos - 1) > LEN(word) THEN
              PRINT " ";: LOCATE YPOS, XPOS - 1
            ENDIF
          LOCATE YPOS, XPOS - 1
          ENDIF
        CASE CHR(5) ' Cursor Right
          IF XPOS < endpos THEN
            IF XPOS - (startpos - 1) < LEN(word) THEN
              LOCATE YPOS, XPOS + 1
            ELSE
              IF XPOS - (startpos - 1) = LEN(word) THEN
                LOCATE YPOS, XPOS + 1
                PRINT inscur;: LOCATE YPOS, XPOS - 1
              ENDIF
            ENDIF
          ENDIF
        CASE CHR(9) ' Cursor Home
          IF XPOS - (startpos - 1) > LEN(word) THEN
            LOCATE YPOS, startpos + LEN(word): PRINT " ";
          ENDIF
          LOCATE YPOS, startpos
        CASE CHR(10) ' Cursor End
          LOCATE YPOS, startpos + LEN(word)
        CASE CHR$(197) ' Android Exit
          LOCATE 3, 0: PRINT "Finished...";
          STOP
      END SELECT
      z = - 1: toggleblink = 1: mycursor
    ELSE
      SELECT CASE mykey
        CASE CHR(8) ' Backspace
          IF XPOS > startpos THEN
            LOCATE YPOS, XPOS - 1
            IF XPOS - (startpos - 1) >= LEN(word) THEN
              word = MID(word, 1, XPOS - (startpos - 1) - 1)
            ELSE
              word = MID(word, 1, XPOS - (startpos - 1) - 1) + MID(word, XPOS - (startpos - 1) + 1)
            ENDIF
            wipe = 1: GOSUB xprint
            IF x - (startpos - 1) > LEN(word) THEN
              PRINT inscur;: LOCATE YPOS, XPOS - 1
            ENDIF
            LOCATE y, x
          ENDIF
        CASE CHR(9) ' Delete via Tab Key
          IF XPOS - (startpos - 1) <= LEN(word) THEN  
            IF XPOS = startpos THEN
              IF LEN(word) > 1 THEN
                word = MID(word, 2)
              ELSE
                word = ""
              ENDIF          
            ELSE
              IF XPOS - (startpos - 1) < LEN(word) THEN
                word = MID(word, 1, XPOS - (startpos - 1) - 1) + MID(word, XPOS - (startpos - 1) + 1)
              ELSE
                word = MID(word, 1, XPOS - (startpos - 1) - 1)  
              ENDIF
            ENDIF
            wipe = 1: GOSUB xprint
            IF x - (startpos - 1) > LEN(word) THEN
              PRINT inscur;: LOCATE YPOS, XPOS - 1
            ENDIF
            LOCATE y, x
          ENDIF
        CASE "`" ' Insert / Overwrite via ` key
          IF inscur = "_" THEN inscur = CHR(32) ELSE inscur = "_"
        CASE CHR(13) ' Print word and quit
          LOCATE 2, 0
          PRINT word;
          STOP
        CASE ELSE ' Type Characters 32 - 126 to the screen
          IF inscur <> "_" AND XPOS < endpos OR inscur = "_" AND startpos + LEN(word) < endpos THEN
            IF mykey >= CHR(32) AND mykey <= CHR(126) THEN
              IF XPOS - (startpos - 1) > LEN(word) THEN
                word = word + mykey
              ELSE
                SELECT CASE inscur
                  CASE " " ' Overwrite
                    IF XPOS = startpos THEN
                      word = mykey + MID(word, XPOS - (startpos - 1) + 1)
                    ELSE
                      IF XPOS - (startpos - 1) >= LEN(word) THEN
                        word = MID(word, 1, XPOS - (startpos - 1) - 1) + mykey
                      ELSE
                        word = MID(word, 1, XPOS - (startpos - 1) - 1) + mykey + MID(word, XPOS - (startpos -1) + 1)
                      ENDIF
                    ENDIF
                  CASE ELSE ' Insert
                    IF XPOS = startpos THEN
                      word = mykey + MID$(word, XPOS - (startpos - 1))
                    ELSE
                      word = MID$(word, 1, XPOS - (startpos - 1) - 1) + mykey + MID$(word, XPOS - (startpos - 1))
                    ENDIF
                END SELECT
              ENDIF
              wipe = 0: GOSUB xprint
              LOCATE YPOS, XPOS + 1
              IF XPOS - (startpos - 1) > LEN(word) THEN
                PRINT inscur;: LOCATE YPOS, XPOS - 1
              ENDIF
            ENDIF
          ENDIF
      END SELECT
      z = -1: toggleblink = 1: mycursor
    ENDIF
  ELSE
    mycursor
  ENDIF
WEND
STOP

label xprint
y = YPOS: x = XPOS
IF wipe THEN LOCATE YPOS, startpos: PRINT SPACE(endpos - startpos + 1);
LOCATE YPOS, startpos
PRINT word;
LOCATE y, x
RETURN

SUB mycursor
  IF ABS(z - TIMER) > .33 or z = -1 THEN
    IF XPOS - (startpos - 1) > LEN(word) THEN
      SELECT CASE inscur
        CASE "_"
          IF toggleblink THEN COLOR 7, 0 ELSE COLOR 0, 0
        CASE ELSE
          IF toggleblink THEN COLOR 0, 8 ELSE COLOR 7, 0
      END SELECT
      PRINT inscur;: LOCATE YPOS, XPOS - 1
    ELSE
      SELECT CASE toggleblink
        CASE 0
          SELECT CASE inscur
            CASE "_"
              PRINT MID(word, XPOS - startpos + 1, 1);
            CASE ELSE
              COLOR 0, 8
              PRINT MID(word, XPOS - startpos + 1, 1);
            END SELECT
        CASE 1
          COLOR 7, 0
          PRINT Uline(MID(word, XPOS - startpos + 1, 1));
          DELAY 35
      END SELECT
      LOCATE YPOS, XPOS - 1
    ENDIF
    z = TIMER
    toggleblink = ABS(toggleblink - 1)
    COLOR 7, 0
  ENDIF
END SUB