Author Topic: Restricted keyboard INPUT routine  (Read 3372 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Restricted keyboard INPUT routine
« on: January 20, 2021, 11:55:26 am »
I'm adding this to my Keys48 program, thought I'd share it here in case someone whats it.

Basically this is like a keyboard INPUT routine that puts a limit on the number of characters you can enter.

- Dav

Code: QB64: [Select]
  1. '==========
  2. 'KINPUT.BAS
  3. '==========
  4. 'A restricted keyboard like INPUT routine.
  5. 'Limits how many characters you can enter.
  6. 'Coded by Dav, JAN/2021
  7.  
  8. a$ = KINPUT$(3, 3, "Enter up to 12 letters: ", 12)
  9.  
  10. PRINT "You entered: "; a$
  11.  
  12.  
  13. FUNCTION KINPUT$ (y, x, text$, limitnum)
  14.   'ENTER returns text entered.
  15.   'Handles the backspace.
  16.   'ESC returns nothing.
  17.  
  18.     LOCATE y, x: PRINT text$;
  19.  
  20.     entry$ = ""
  21.     y = CSRLIN: x = POS(1)
  22.  
  23.     DO
  24.         a$ = INPUT$(1)
  25.  
  26.         IF a$ = CHR$(13) THEN 'enter returns entry
  27.             KINPUT$ = entry$: EXIT FUNCTION
  28.         END IF
  29.  
  30.         IF a$ = CHR$(27) THEN 'ESC bails out
  31.             KINPUT$ = "": EXIT FUNCTION
  32.         END IF
  33.  
  34.         IF a$ = CHR$(8) THEN 'Backspace goes back a space
  35.             IF LEN(entry$) > 0 THEN
  36.                 entry$ = MID$(entry$, 1, LEN(entry$) - 1)
  37.             END IF
  38.         ELSE
  39.             'add letter entered, if not over limitnum
  40.             IF LEN(entry$) < limitnum THEN
  41.                 entry$ = entry$ + a$
  42.             END IF
  43.         END IF
  44.  
  45.         LOCATE y, x: PRINT SPACE$(limitnum);
  46.         LOCATE y, x: PRINT entry$;
  47.  
  48.     LOOP
  49.  
  50.  
« Last Edit: January 20, 2021, 11:56:59 am by Dav »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #1 on: January 20, 2021, 12:03:11 pm »
I don't like being limited. That's why I only use SCREEN 0.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

FellippeHeitor

  • Guest
Re: Restricted keyboard INPUT routine
« Reply #2 on: January 20, 2021, 12:04:53 pm »
I'm yet to try your latest musical piece, Dav! Will do soon.

I'd have used another do/loop to get keyboard input. Your approach with INPUT$() is much cleaner (I hardly ever remember the function syntax of INPUT$()). Thanks for sharing!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #3 on: January 20, 2021, 10:11:18 pm »
Useful, simple and expands my syntax vocabulary. Filed in my "Dav" folder. Thanks.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #4 on: January 20, 2021, 11:27:22 pm »
Thanks!  I made a graphical version too, called INPUTBOX, which I probably will use instead.  It saves/restores the screen, and returns to original cursor position.

Draws a little box based on total text length.

- Dav

Code: QB64: [Select]
  1. '=============
  2. 'INPUTBOX$.BAS
  3. '=============
  4. 'Graphical restricted INPUT Box.
  5. 'Coded by Dav, JAN/2021
  6.  
  7. '=== Set screen mode, and color
  8. SCREEN _NEWIMAGE(600, 600, 32)
  9.  
  10. CLS , _RGB(32, 32, 32)
  11.  
  12. '=== draw stuff
  13. FOR x = 1 TO 600 STEP 3
  14.     FOR y = 1 TO 600 STEP 3
  15.         PSET (x, y), _RGB(RND * 255, RND * 255, RND * 255)
  16.     NEXT
  17.  
  18.  
  19. a$ = INPUTBOX$(15, 15, "ENTER SOMETHING: ", 22, _RGB(255, 255, 255), _RGB(70, 95, 114))
  20.  
  21. IF a$ <> "" THEN
  22.     PRINT "You entered: "; a$
  23.     PRINT "You entered nothing."
  24.  
  25.  
  26.  
  27. FUNCTION INPUTBOX$ (y, x, text$, limitnum, fg&, bg&)
  28.  
  29.     '=== save original place of cursor
  30.     origy = CSRLIN: origx = POS(1)
  31.  
  32.     '=== Save whole screen
  33.     DIM scr1 AS _MEM, scr2 AS _MEM
  34.     scr1 = _MEMIMAGE(0): scr2 = _MEMNEW(scr1.SIZE)
  35.     _MEMCOPY scr1, scr1.OFFSET, scr1.SIZE TO scr2, scr2.OFFSET
  36.  
  37.     tl = LEN(text$) + limitnum 'total length of letter spacing used
  38.  
  39.     LINE (x * 8 - 24, y * 16 - 32)-((x * 8) + (tl * 8) + 8, y * 16 + 16), bg&, BF
  40.     LINE (x * 8 - 24, y * 16 - 32)-((x * 8) + (tl * 8) + 8, y * 16 + 16), fg&, B
  41.     COLOR fg&, bg&
  42.  
  43.     LOCATE y, x: PRINT text$;
  44.     y = CSRLIN: x = POS(1)
  45.  
  46.     entry$ = ""
  47.  
  48.     DO
  49.         a$ = INPUT$(1)
  50.  
  51.         '=== Return exits with data
  52.         IF a$ = CHR$(13) THEN INPUTBOX$ = entry$: EXIT DO
  53.         '=== ESC cancels
  54.         IF a$ = CHR$(27) THEN INPUTBOX$ = "": EXIT DO
  55.         '=== Handle backspace
  56.         IF a$ = CHR$(8) THEN 'Backspace goes back a space
  57.             IF LEN(entry$) > 0 THEN
  58.                 entry$ = MID$(entry$, 1, LEN(entry$) - 1)
  59.             END IF
  60.         ELSE
  61.             'add letter entered, if not over limitnum
  62.             IF LEN(entry$) < limitnum THEN
  63.                 entry$ = entry$ + a$
  64.             END IF
  65.         END IF
  66.  
  67.         LOCATE y, x: PRINT SPACE$(limitnum);
  68.         LOCATE y, x: PRINT entry$;
  69.  
  70.     LOOP
  71.  
  72.     '=== Restore the whole screen
  73.     _MEMCOPY scr2, scr2.OFFSET, scr2.SIZE TO scr1, scr1.OFFSET
  74.     _MEMFREE scr1: _MEMFREE scr2
  75.  
  76.     '=== restore original y,x
  77.     LOCATE origy, origx
  78.  
  79.  
  80.  

« Last Edit: January 20, 2021, 11:46:36 pm by Dav »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #5 on: January 21, 2021, 09:03:36 am »
Worked the graphics version out to more what I was looking for.  IBOX$ Function performs 2 tasks - either gets user input, or shows an info box.  It automatically centers the box on the screen based on text length.

If limitnum = 0 then it just displays text$. If you set the limit number, it will return user input.

- Dav

Code: QB64: [Select]
  1. '=============
  2. 'IBOX$.BAS
  3. '=============
  4. 'INPUT & INFO line box.
  5. 'Either get user info, or show info.
  6. 'Coded by Dav, JAN/2021
  7.  
  8. '=== Set screen mode, and color
  9. SCREEN _NEWIMAGE(600, 600, 32)
  10.  
  11. CLS , _RGB(32, 32, 32)
  12.  
  13. '=== draw stuff
  14. FOR x = 1 TO 600 STEP 3
  15.     FOR y = 1 TO 600 STEP 3
  16.         PSET (x, y), _RGB(RND * 255, RND * 255, RND * 255)
  17.     NEXT
  18.  
  19. a$ = IBOX$("ENTER SOMETHING: ", 22, _RGB(255, 255, 255), _RGB(70, 95, 114))
  20.  
  21. b$ = IBOX$("YOU ENTERED: " + a$, 0, _RGB(255, 255, 255), _RGB(70, 95, 114))
  22.  
  23.  
  24.  
  25. FUNCTION IBOX$ (text$, limitnum, fg&, bg&)
  26.     'This function either gets user input or shows info.
  27.     'If limitnum is 0, it just shows info.
  28.  
  29.     'text$: Your text to show
  30.     'limitnum: how many letters of input to get
  31.     'fg&: The Text color
  32.     'bg&: Background color of box
  33.  
  34.     '=== save original place of cursor
  35.     origy = CSRLIN: origx = POS(1)
  36.  
  37.     '=== Save whole screen
  38.     DIM scr1 AS _MEM, scr2 AS _MEM
  39.     scr1 = _MEMIMAGE(0): scr2 = _MEMNEW(scr1.SIZE)
  40.     _MEMCOPY scr1, scr1.OFFSET, scr1.SIZE TO scr2, scr2.OFFSET
  41.  
  42.     '=== find center x/y of screen
  43.     y = INT(_HEIGHT / _FONTHEIGHT / 2)
  44.     x = INT(_WIDTH / _FONTWIDTH / 2)
  45.  
  46.     tl = LEN(text$) + limitnum 'total length of letter spacing used
  47.     x = x - INT(tl / 2) 'recompute x based on text length
  48.  
  49.     LINE (x * 8 - 24, y * 16 - 32)-((x * 8) + (tl * 8) + 8, y * 16 + 16), bg&, BF
  50.     LINE (x * 8 - 24, y * 16 - 32)-((x * 8) + (tl * 8) + 8, y * 16 + 16), fg&, B
  51.     COLOR fg&, bg&
  52.  
  53.     LOCATE y, x: PRINT text$;
  54.     y = CSRLIN: x = POS(1)
  55.  
  56.     IF limitnum = 0 THEN
  57.         a$ = INPUT$(1)
  58.         IBOX$ = ""
  59.     ELSE
  60.  
  61.         entry$ = ""
  62.  
  63.         DO
  64.             a$ = INPUT$(1)
  65.  
  66.             '=== Return exits with data
  67.             IF a$ = CHR$(13) THEN IBOX$ = entry$: EXIT DO
  68.             '=== ESC cancels, returns nothing
  69.             IF a$ = CHR$(27) THEN IBOX$ = "": EXIT DO
  70.             '=== Backspace handle
  71.             IF a$ = CHR$(8) THEN 'Backspace goes back a space
  72.                 IF LEN(entry$) > 0 THEN
  73.                     entry$ = MID$(entry$, 1, LEN(entry$) - 1)
  74.                 END IF
  75.             ELSE
  76.                 'add letter entered, if not over limitnum
  77.                 IF LEN(entry$) < limitnum THEN
  78.                     entry$ = entry$ + a$
  79.                 END IF
  80.             END IF
  81.  
  82.             LOCATE y, x: PRINT SPACE$(limitnum);
  83.             LOCATE y, x: PRINT entry$;
  84.  
  85.         LOOP
  86.  
  87.     END IF
  88.  
  89.     '=== Restore the whole screen
  90.     _MEMCOPY scr2, scr2.OFFSET, scr2.SIZE TO scr1, scr1.OFFSET
  91.     _MEMFREE scr1: _MEMFREE scr2
  92.  
  93.     '=== restore original y,x
  94.     LOCATE origy, origx
  95.  
  96.  
« Last Edit: January 21, 2021, 09:59:22 am by Dav »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #6 on: January 21, 2021, 12:51:05 pm »
I have an InputBox$ you can grab title bar and move around screen:

Code: QB64: [Select]
  1. _TITLE "inputBox$ tester.bas started 2018-10-26 need an input box for WS Editor"
  2. ' 2019-07-32 assimulate scnState(restoreTF) used to save and restore screen settings
  3. ' so sub can do it's thing and restore settings, Thanks Steve McNeill for starter code and idea.
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6. _SCREENMOVE 100, 20
  7. DIM well$, enter$, k$, kh AS LONG
  8.  
  9. COLOR &HFFFFFF00, &HFF880000
  10. PRINT "Here is some stuff on screen."
  11. well$ = inputBox$("Well?", "Test inputBox$", 20)
  12. PRINT "inputBox$ returned: "; well$; ". Is this line printing exactly below last stuff sentence?" ' OK now with center fix too!
  13. INPUT "OK? enter for next test, use h or m keypress to invoke inputBox$...", enter$
  14.  
  15. 'draw stuff, until h or m press, then show message box
  16.     k$ = INKEY$
  17.     IF k$ = "m" OR k$ = "h" THEN
  18.         well$ = inputBox$("Well?", "Test call inputBox", 36)
  19.         PRINT "inputBox$() returned: *"; well$; "*"
  20.     END IF
  21.     'kh = 0  'should not need this to stop esc keypress in input box
  22.     LINE (RND * _WIDTH, RND * _HEIGHT)-STEP(RND * 80, RND * 60), _RGB32(RND * 255, RND * 255, RND * 255), BF
  23.     kh = _KEYHIT
  24.     IF kh = 27 THEN EXIT WHILE
  25.     '_DISPLAY   '<< should not need this
  26.     _LIMIT 5
  27. PRINT "OK where is this print line going to end up, hopefully under the last inputBox returned." 'yes! Excellent!
  28. PRINT "InputBox$() last returned: "; well$; ",  Goodbye!"
  29.  
  30. ' You can grab this box by title and drag it around screen for full viewing while answering prompt.
  31. ' Only one line allowed for prompt$
  32. ' boxWidth is 4 more than the allowed length of input, it needs to be longer than title$ and prompt$ also
  33. ' Utilities > Input Box > Input Box 1 tester v 2019-07-31
  34. FUNCTION inputBox$ (prompt$, title$, boxWidth AS _BYTE)
  35.     DIM ForeColor AS _UNSIGNED LONG, BackColor AS _UNSIGNED LONG, White AS _UNSIGNED LONG
  36.     DIM sw AS INTEGER, sh AS INTEGER, curScrn AS LONG, backScrn AS LONG, ibx AS LONG 'some handles
  37.  
  38.     'colors
  39.     ForeColor = &HFF000055 '<  change as desired  prompt text color, back color or type in area
  40.     BackColor = &HFF6080CC '<  change as desired  used fore color in type in area
  41.     White = &HFFFFFFFF
  42.  
  43.     'items to restore at exit
  44.     scnState 0
  45.  
  46.     'screen snapshot
  47.     sw = _WIDTH: sh = _HEIGHT: curScrn = _DEST
  48.     backScrn = _NEWIMAGE(sw, sh, 32)
  49.     _PUTIMAGE , curScrn, backScrn
  50.  
  51.     'moving box around on screen
  52.     DIM bxW AS INTEGER, bxH AS INTEGER
  53.     DIM mb AS INTEGER, mx AS INTEGER, my AS INTEGER, mi AS INTEGER, grabx AS INTEGER, graby AS INTEGER
  54.     DIM tlx AS INTEGER, tly AS INTEGER 'top left corner of message box
  55.     DIM lastx AS INTEGER, lasty AS INTEGER
  56.     DIM INP$, kh&
  57.  
  58.     'draw message box
  59.     bxW = boxWidth * 8: bxH = 7 * 16
  60.     ibx = _NEWIMAGE(bxW, bxH, 32)
  61.     _DEST ibx
  62.     COLOR &HFF880000, White
  63.     LOCATE 1, 1: PRINT LEFT$(SPACE$(INT((boxWidth - LEN(title$) - 3)) / 2) + title$ + SPACE$(boxWidth), boxWidth)
  64.     COLOR White, &HFFBB0000
  65.     LOCATE 1, boxWidth - 2: PRINT " X "
  66.     COLOR ForeColor, BackColor
  67.     LOCATE 2, 1: PRINT SPACE$(boxWidth);
  68.     LOCATE 3, 1: PRINT LEFT$(SPACE$((boxWidth - LEN(prompt$)) / 2) + prompt$ + SPACE$(boxWidth), boxWidth);
  69.     LOCATE 4, 1: PRINT SPACE$(boxWidth);
  70.     LOCATE 5, 1: PRINT SPACE$(boxWidth);
  71.     LOCATE 6, 1: PRINT SPACE$(boxWidth);
  72.     INP$ = ""
  73.     GOSUB finishBox
  74.  
  75.     'convert to pixels the top left corner of box at moment
  76.     bxW = boxWidth * 8: bxH = 5 * 16
  77.     tlx = (sw - bxW) / 2: tly = (sh - bxH) / 2
  78.     lastx = tlx: lasty = tly
  79.     _KEYCLEAR
  80.     'now allow user to move it around or just read it
  81.     WHILE 1
  82.         CLS
  83.         _PUTIMAGE , backScrn
  84.         _PUTIMAGE (tlx, tly), ibx, curScrn
  85.         _DISPLAY
  86.         WHILE _MOUSEINPUT: WEND
  87.         mx = _MOUSEX: my = _MOUSEY: mb = _MOUSEBUTTON(1)
  88.         IF mb THEN
  89.             IF mx >= tlx AND mx <= tlx + bxW AND my >= tly AND my <= tly + 16 THEN 'mouse down on title bar
  90.                 IF mx >= tlx + bxW - 24 THEN EXIT WHILE
  91.                 grabx = mx - tlx: graby = my - tly
  92.                 DO WHILE mb 'wait for release
  93.                     mi = _MOUSEINPUT: mb = _MOUSEBUTTON(1)
  94.                     mx = _MOUSEX: my = _MOUSEY
  95.                     IF mx - grabx >= 0 AND mx - grabx <= sw - bxW AND my - graby >= 0 AND my - graby <= sh - bxH THEN
  96.                         'attempt to speed up with less updates
  97.                         IF ((lastx - (mx - grabx)) ^ 2 + (lasty - (my - graby)) ^ 2) ^ .5 > 10 THEN
  98.                             tlx = mx - grabx: tly = my - graby
  99.                             CLS
  100.                             _PUTIMAGE , backScrn
  101.                             _PUTIMAGE (tlx, tly), ibx, curScrn
  102.                             lastx = tlx: lasty = tly
  103.                             _DISPLAY
  104.                         END IF
  105.                     END IF
  106.                     _LIMIT 400
  107.                 LOOP
  108.             END IF
  109.         END IF
  110.         kh& = _KEYHIT
  111.         SELECT CASE kh& 'whew not much for the main event!
  112.             CASE 13: EXIT WHILE
  113.             CASE 27: INP$ = "": EXIT WHILE
  114.             CASE 32 TO 128: IF LEN(INP$) < boxWidth - 4 THEN INP$ = INP$ + CHR$(kh&): GOSUB finishBox ELSE BEEP
  115.             CASE 8: IF LEN(INP$) THEN INP$ = LEFT$(INP$, LEN(INP$) - 1): GOSUB finishBox ELSE BEEP
  116.         END SELECT
  117.  
  118.         _LIMIT 60
  119.     WEND
  120.  
  121.     'put things back
  122.     scnState 1 'need fg and bg colors set to cls
  123.     CLS '? is this needed YES!!
  124.     _PUTIMAGE , backScrn
  125.     _DISPLAY
  126.     _FREEIMAGE backScrn
  127.     _FREEIMAGE ibx
  128.     scnState 1 'because we have to call _display, we have to call this again
  129.     inputBox$ = INP$
  130.  
  131.     finishBox:
  132.     _DEST ibx
  133.     COLOR BackColor, ForeColor
  134.     LOCATE 5, 2: PRINT LEFT$(" " + INP$ + SPACE$(boxWidth - 2), boxWidth - 2)
  135.     _DEST curScrn
  136.     RETURN
  137.  
  138. 'from mBox v 2019-07-31 update
  139. ' for saving and restoring screen settins
  140. SUB scnState (restoreTF AS INTEGER) 'Thanks Steve McNeill
  141.     STATIC Font AS LONG, DefaultColor AS _UNSIGNED LONG, BackGroundColor AS _UNSIGNED LONG, Dest AS LONG, Source AS LONG
  142.     STATIC row AS INTEGER, col AS INTEGER, autodisplay AS INTEGER, mb AS INTEGER
  143.     IF restoreTF THEN
  144.         _FONT Font
  145.         COLOR DefaultColor, BackGroundColor
  146.         _DEST Dest
  147.         _SOURCE Source
  148.         LOCATE row, col
  149.         IF autodisplay THEN _AUTODISPLAY ELSE _DISPLAY
  150.         _KEYCLEAR
  151.         WHILE _MOUSEINPUT: WEND 'clear mouse clicks
  152.         mb = _MOUSEBUTTON(1)
  153.         IF mb THEN
  154.             DO
  155.                 WHILE _MOUSEINPUT: WEND
  156.                 mb = _MOUSEBUTTON(1)
  157.                 _LIMIT 100
  158.             LOOP UNTIL mb = 0
  159.         END IF
  160.     ELSE
  161.         Font = _FONT: DefaultColor = _DEFAULTCOLOR: BackGroundColor = _BACKGROUNDCOLOR
  162.         Dest = _DEST: Source = _SOURCE
  163.         row = CSRLIN: col = POS(0): autodisplay = _AUTODISPLAY
  164.     END IF
  165.  
  166.  
  167.  
  168.  

Also has brother mBox SUB for messages:
https://www.qb64.org/forum/index.php?topic=1511.msg107661#msg107661
« Last Edit: January 21, 2021, 12:57:44 pm by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #7 on: January 21, 2021, 01:05:03 pm »
Those are nicely done, @bplus!  Will come in handy!  I'm saving them.

- Dav

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #8 on: January 21, 2021, 02:36:40 pm »
Hey Dav, think BIGGER! Go with screen 0!

https://www.qb64.org/forum/index.php?topic=3520.0

Includes lateral scrolling, highlighting, cut, copy, paste, arrow, home, end, ctrl, overwrite, insert, and mouse functions.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #9 on: January 21, 2021, 02:41:33 pm »
Yeah, I was checking that out, Pete! Looks good, i will test it out...

-Dav

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Restricted keyboard INPUT routine
« Reply #10 on: January 21, 2021, 03:02:19 pm »
I wonder what others, not you, I know you made a text editor, but people who would like to have a way to input and manipulate a line of text, think when they see the code needed to achieve that ability, which may be as large, or larger, than their project.

Well if you do get a chance to take it for a spin, I'd love to discuss any differences you may have had in your approach to text input and editing in your IDE.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/