Author Topic: How to leave an input line.  (Read 5780 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
How to leave an input line.
« on: June 11, 2021, 02:42:41 am »
I have a problem. My program (I have attached an image as example) is almost finished, but at the moment is a flow. I have to introduce the possibility to navigate through it and then write the results to files.

The problem is this.
The program at the start asks some input:

1) Enter input 1:      B
2) Enter input 2:      B
3) Enter input 3:      C
4) Enter input 4:      _


Suppose that the program is now asking input 4 and that we have just realized that input 1 is wrong, because is not B, but A.

I want to do this: just pressing up arrow (without enter), the program must leaves the input line corresponding to input 4 and go up, to line asking input 3, and so on (possibly preserving input 2 and 3).
« Last Edit: June 11, 2021, 02:47:00 am by bartok »

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: How to leave an input line.
« Reply #1 on: June 11, 2021, 05:39:17 am »

Not very elegant, but for example you could imagine something like that:


1000 Input "blabla A (or -1 to cancel)", a1$: If a1$ = "-1" Then Stop
1001 Input "blabla B (or -1 -> asking A)", b1$: If b1$ = "-1" Then GoTo 1000
1002 Input "blabla C (or -1 -> asking B)", c1$: If c1$ = "-1" Then GoTo 1001
1003 Input "blabla D (or -1 -> asking C)", d1$: If d1$ = "-1" Then GoTo 1002
Why not yes ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to leave an input line.
« Reply #2 on: June 11, 2021, 09:28:42 am »
EDIT missed this part:
Quote
I want to do this: just pressing up arrow (without enter), the program must leaves the input line corresponding to input 4 and go up, to line asking input 3, and so on (possibly preserving input 2 and 3).
« Last Edit: June 11, 2021, 03:08:22 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to leave an input line.
« Reply #3 on: June 11, 2021, 11:36:33 am »
EDIT missed this part:
Quote
I want to do this: just pressing up arrow (without enter), the program must leaves the input line corresponding to input 4 and go up, to line asking input 3, and so on (possibly preserving input 2 and 3).
« Last Edit: June 11, 2021, 03:08:41 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to leave an input line.
« Reply #4 on: June 11, 2021, 11:54:21 am »
EDIT missed this part:
Quote
I want to do this: just pressing up arrow (without enter), the program must leaves the input line corresponding to input 4 and go up, to line asking input 3, and so on (possibly preserving input 2 and 3).
« Last Edit: June 11, 2021, 03:08:53 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How to leave an input line.
« Reply #5 on: June 11, 2021, 12:57:07 pm »
Terry Ritchie’s GLInput Library does what you’re looking for, and is well documented, if you have a copy of it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: How to leave an input line.
« Reply #6 on: June 11, 2021, 09:58:52 pm »
@bartok


Here is a basic program that shows what you want. It takes a lot more coding to make it work properly.


Code: QB64: [Select]
  1. DIM msg(1) AS STRING
  2.  
  3. msg(0) = "Enter name "
  4. msg(1) = "Enter date "
  5.  
  6. LL1:
  7. FOR X = 0 TO 1
  8.     IF INKEY$ = CHR$(27) THEN END
  9.     LOCATE 10 + X, 2 + LEN(msg(X)): PRINT SPACE$(10)
  10.     LOCATE 10 + X, 2: PRINT msg(X);: INPUT S(X)
  11.  
  12.  
  13.     i$ = INKEY$
  14.     IF i$ = CHR$(27) THEN END
  15.     IF i$ = CHR$(13) THEN GOTO LL1
  16.  


Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: How to leave an input line.
« Reply #7 on: June 11, 2021, 10:23:10 pm »
A custom input routine would probably be your best bet for that kind of flexibility.
Based around a DO:LOOP structure without GOTO. It will take me a bit to write you up an example but if your willing to wait.....
Granted after becoming radioactive I only have a half-life!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: How to leave an input line.
« Reply #8 on: June 11, 2021, 11:59:49 pm »
Not super refined but is something like this what you want?

Code: QB64: [Select]
  1. 'Editable input example
  2. 'lets start with some base constants
  3. CONST Key_Right = 19712, Key_Left = 19200, Key_Up = 18432, Key_Down = 20480
  4. CONST TRUE = -1, FALSE = NOT TRUE
  5.  
  6. DIM Inputs(10) AS STRING 'for this example we will allow up to 10 inputs
  7.  
  8. 'some prep work
  9. Opt%% = 1 'start on the first input line
  10.  
  11. 'start our loop up
  12.  KBD& = _KEYHIT 'keyboard input
  13.  IF KBD& < 0 THEN KBD& = 0 'negate any negative keyhit values
  14.  FOR I%% = 1 TO 10 'loop through all our input options
  15.   'double space the input lines for readability
  16.   LOCATE I%% * 2, 1: PRINT "INPUT VALUE #"; I%%; "  -"; Inputs(I%%);
  17.   'print a "cursor" to show which input user is on, use space$ to clear other lines
  18.   IF I%% = Opt%% THEN PRINT CHR$(176) ELSE PRINT SPACE$(2)
  19.  NEXT I%%
  20.  
  21.  'let us process which line the user is inputing
  22.  SELECT CASE KBD&
  23.   CASE 13 'enter to accept inputs
  24.    ExitFlag%% = TRUE
  25.   CASE 8 'backspace to clear this input
  26.    Inputs(Opt%%) = ""
  27.   CASE Key_Up
  28.    Opt%% = Opt%% - 1
  29.    'for this example we will stop on 1, we could always cycle to 10 though
  30.    IF Opt%% = 0 THEN Opt%% = 1
  31.   CASE Key_Down
  32.    Opt%% = Opt%% + 1
  33.    'we will cap off at 10, though we could cycle back to 1
  34.    IF Opt%% = 11 THEN Opt%% = 10
  35.   CASE 32 TO 122
  36.    'now we are only going to allow 1 character inputs for this example
  37.    Inputs(Opt%%) = CHR$(KBD&)
  38.   CASE NOT 0 ' any other input(0 is no input)
  39.    BEEP 'invalid input
  40.  '-----------------------------------------------
  41.  
  42.  _LIMIT 30 'up date 30times a sec to save CPU use
  43.  IF KBD& = 27 THEN ExitFlag%% = TRUE 'exit this example
  44. LOOP UNTIL ExitFlag%%
« Last Edit: June 12, 2021, 11:22:36 am by Cobalt »
Granted after becoming radioactive I only have a half-life!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: How to leave an input line.
« Reply #9 on: June 12, 2021, 02:36:14 am »
@Cobalt
I like the way you combined  a FOR NEXT  inside a DO LOOP

Here are a few mods.  Can enter text . Backspace works

up / down keys rotate now.

Code: QB64: [Select]
  1. 'Editable input example
  2. 'lets start with some base constants
  3. CONST Key_Right = 19712, Key_Left = 19200, Key_Up = 18432, Key_Down = 20480
  4. CONST TRUE = -1, FALSE = NOT TRUE
  5.  
  6. DIM Inputs(9) AS STRING 'for this example we will allow up to 10 inputs
  7.  
  8.  
  9. 'some prep work
  10. OPT = 0 'start on the first input line
  11.  
  12. 'start our loop up
  13.     KH = _KEYHIT 'keyboard input
  14.     IF KH < 0 THEN KH = 0 'negate any negative keyhit values
  15.  
  16.     FOR I = 0 TO 9 'loop through all our input options
  17.  
  18.         'double space the input lines for readability
  19.         LOCATE 1 + (I * 2), 1: PRINT "INPUT VALUE #"; I; "  -"; Inputs(I);
  20.  
  21.         'print a "cursor" to show which input user is on, use space$ to clear other lines
  22.         IF I = OPT THEN PRINT CHR$(176) + " " ELSE PRINT SPACE$(2)
  23.  
  24.     NEXT I
  25.  
  26.     'let us process which line the user is inputing
  27.     SELECT CASE KH
  28.         CASE 13 'enter to accept inputs
  29.             ExitFlag%% = TRUE
  30.         CASE 8 'backspace to clear this input
  31.  
  32.             Inputs(OPT) = LEFT$(Inputs(OPT), LEN(Inputs(OPT)) - 1)
  33.  
  34.         CASE Key_Up
  35.             IF OPT = 0 THEN OPT = 10
  36.             IF OPT > 0 THEN OPT = OPT - 1
  37.  
  38.         CASE Key_Down
  39.  
  40.             IF OPT < 10 THEN OPT = OPT + 1
  41.             IF OPT = 10 THEN OPT = 0
  42.  
  43.         CASE 32 TO 122
  44.             'now we are only going to allow 1 character inputs for this example
  45.             Inputs(OPT) = Inputs(OPT) + CHR$(KH)
  46.         CASE NOT 0 ' any other input(0 is no input)
  47.             BEEP 'invalid input
  48.     END SELECT
  49.     '-----------------------------------------------
  50.  
  51.     _LIMIT 30 'up date 30times a sec to save CPU use
  52.     IF KH = 27 THEN ExitFlag%% = TRUE 'exit this example
  53. LOOP UNTIL ExitFlag%%
  54.  

Added blinking cursor


Code: QB64: [Select]
  1. 'Editable input example
  2. 'lets start with some base constants
  3. CONST Key_Right = 19712, Key_Left = 19200, Key_Up = 18432, Key_Down = 20480
  4. CONST TRUE = -1, FALSE = NOT TRUE
  5.  
  6. DIM Inputs(9) AS STRING 'for this example we will allow up to 10 inputs
  7. DIM cp(30)
  8.  
  9.  
  10. 'some prep work
  11. OPT = 0 'start on the first input line
  12.  
  13. 'start our loop up
  14.     KH = _KEYHIT 'keyboard input
  15.     IF KH < 0 THEN KH = 0 'negate any negative keyhit values
  16.  
  17.     FOR I = 0 TO 9 'loop through all our input options
  18.  
  19.         'double space the input lines for readability
  20.         LOCATE 1 + (I * 2), 1: PRINT "INPUT VALUE #"; I; Inputs(I)
  21.  
  22.         IF I = OPT THEN
  23.             LOCATE 1 + (OPT * 2), cp(OPT) + 17: PRINT " "
  24.         END IF
  25.  
  26.     NEXT I
  27.     LOCATE 1 + (OPT * 2), cp(OPT) + 17, 1, 31, 31
  28.  
  29.  
  30.     'let us process which line the user is inputing
  31.     SELECT CASE KH
  32.  
  33.         CASE 13 'enter to accept inputs
  34.             ExitFlag%% = TRUE
  35.  
  36.         CASE 8 'backspace to clear this input
  37.             IF cp(OPT) > 0 THEN
  38.                 Inputs(OPT) = LEFT$(Inputs(OPT), LEN(Inputs(OPT)) - 1)
  39.                 cp(OPT) = cp(OPT) - 1
  40.             END IF
  41.  
  42.         CASE Key_Up
  43.             IF OPT = 0 THEN OPT = 10
  44.             IF OPT > 0 THEN OPT = OPT - 1
  45.  
  46.         CASE Key_Down
  47.  
  48.             IF OPT < 10 THEN OPT = OPT + 1
  49.             IF OPT = 10 THEN OPT = 0
  50.  
  51.         CASE 32 TO 122
  52.             'now we are only going to allow 1 character inputs for this example
  53.             Inputs(OPT) = Inputs(OPT) + CHR$(KH)
  54.             cp(OPT) = cp(OPT) + 1
  55.         CASE NOT 0 ' any other input(0 is no input)
  56.             BEEP 'invalid input
  57.     END SELECT
  58.     '-----------------------------------------------
  59.  
  60.     _LIMIT 30 'up date 30times a sec to save CPU use
  61.     IF KH = 27 THEN ExitFlag%% = TRUE 'exit this example
  62. LOOP UNTIL ExitFlag%%
  63.  
« Last Edit: June 12, 2021, 05:12:54 am by NOVARSEG »

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: How to leave an input line.
« Reply #10 on: June 15, 2021, 02:39:05 am »
Cobalt and NOVARSEG,
thank you very much to both! It almost seems what I am searching. The idea to create a custom input routine is sharp. I have to study it in ordet to apply it to the program. Why I has a bouble %%? I know as variable definition $, %, &, !, but %%?

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: How to leave an input line.
« Reply #11 on: June 15, 2021, 05:31:06 am »
@bartok

From the QB64  IDE Help > Alphabetical  (a few "pages" back from the very end)




    Variable Name Type Suffixes
þ $ STRING text character type: 1 byte
þ ! SINGLE floating decimal point numerical type (4 bytes)
þ # DOUBLE floating decimal point numerical type (8 bytes)
þ ## _FLOAT QB64 decimal point numerical type (32 bytes)
þ ~ _UNSIGNED QB64 whole positive numerical type when it precedes the 6 numerical
  suffixes below:
þ % INTEGER whole numerical type (2 bytes)
þ & LONG whole numerical type (4 bytes}
þ && _INTEGER64 QB64 whole numerical type (8 bytes)
þ ` _BIT QB64 whole numerical type (1 bit) (Key below tilde (~) or CHR$(96))
þ %% _BYTE QB64 whole numerical type (1 byte)
þ %& _OFFSET QB64 whole numerical pointer address type (any byte size required)

Hope this helps

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to leave an input line.
« Reply #12 on: June 15, 2021, 07:45:54 am »
Nice @Cobalt and @NOVARSEG

I made some mods for how I would like to use:
Code: QB64: [Select]
  1. Option _Explicit ' to remind what I haven't dim'd
  2. Dim As Long cursor, nInputs, i
  3. Dim k$
  4. 'setups
  5. nInputs = 6 'for this example we will allow up to nInputs inputs
  6. Dim Inputs$(1 To nInputs)
  7. Dim prompts$(1 To nInputs)
  8. prompts$(1) = "First Name: "
  9. prompts$(2) = " Last Name: "
  10. prompts$(3) = "Home Phone: "
  11. prompts$(4) = "Cell Phone: "
  12. prompts$(5) = "Birth date: "
  13. prompts$(6) = "     State: "
  14. cursor = 1 'start on the first input line
  15. 'start our loop up
  16.     k$ = InKey$ 'keyboard input
  17.     For i = 1 To nInputs 'loop through all our input options
  18.         Locate i, 1: Print prompts$(i); Inputs$(i);
  19.         'print a "cursor" to show which input user is on, use space$ to clear other lines
  20.         If i = cursor Then Print "_" + " " Else Print Space$(2)
  21.     Next
  22.     If Len(k$) Then 'let us process which line the user is inputing
  23.         Select Case Asc(Right$(k$, 1))
  24.             Case 13: If cursor = nInputs Then Exit Do Else cursor = cursor + 1 ' no exit unless on last line
  25.             Case 8: Inputs$(cursor) = Left$(Inputs$(cursor), Len(Inputs$(cursor)) - 1) 'backspace to clear this input
  26.             Case 27: Exit Do
  27.             Case 72: If cursor > 1 Then cursor = cursor - 1 ' up
  28.             Case 80: If cursor < nInputs Then cursor = cursor + 1 'down
  29.             Case 32 TO 122: Inputs$(cursor) = Inputs$(cursor) + k$
  30.         End Select
  31.     End If
  32.     _Limit 30 'up date 30 times a sec to save CPU use
  33. 'show results of inputs
  34. For i = 1 To nInputs
  35.     Print i, prompts$(i); "= "; Inputs$(i)
  36.  
  37.  

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: How to leave an input line.
« Reply #13 on: June 15, 2021, 09:01:10 am »
Nice little snippet, @bplus.  Saving it for future use.  I added CASE 73, 75, 77, 81: 'don't print these to the SELECT CASE ASC(RIGHT$(k$, 1)) area to not show those keys.

- Dav

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to leave an input line.
« Reply #14 on: June 15, 2021, 09:46:09 am »
Hi @Dav

Yeah, the working in of the cursor was simple but genius, NOVARSEG or Cobalt?, in the display update loop,

75, 77 left and right arrow? more up and down? or are we really getting into editing :)

73, 81 pgUp and pgDn make sense, you could start thinking of over a screen full of inputs!

Update:
Code: QB64: [Select]
  1. Option _Explicit ' to remind what I haven't dim'd  B+ mod Cobalt and NOVARSEG and Dav 2021-06-15
  2. Dim As Long cursor, nInputs, i
  3. Dim k$
  4. 'setups
  5. nInputs = 6 'for this example we will allow up to nInputs inputs
  6. Dim Inputs$(1 To nInputs)
  7. Dim prompts$(1 To nInputs)
  8. prompts$(1) = "First Name: "
  9. prompts$(2) = " Last Name: "
  10. prompts$(3) = "Home Phone: "
  11. prompts$(4) = "Cell Phone: "
  12. prompts$(5) = "Birth date: "
  13. prompts$(6) = "     State: "
  14. cursor = 1 'start on the first input line
  15. 'start our loop up
  16.     k$ = InKey$ 'keyboard input
  17.     For i = 1 To nInputs 'loop through all our input options
  18.         Locate i, 1: Print prompts$(i); Inputs$(i);
  19.         'print a "cursor" to show which input user is on, use space$ to clear other lines
  20.         If i = cursor Then Print "_" + " " Else Print Space$(2)
  21.     Next
  22.     If Len(k$) Then 'let us process which line the user is inputing
  23.         Select Case Asc(Right$(k$, 1))
  24.             Case 13: If cursor = nInputs Then Exit Do Else cursor = cursor + 1 ' no exit unless on last line
  25.             Case 8: Inputs$(cursor) = Left$(Inputs$(cursor), Len(Inputs$(cursor)) - 1) 'backspace to clear this input
  26.             Case 27: Exit Do
  27.             Case 72, 75: If cursor > 1 Then cursor = cursor - 1 ' up
  28.             Case 73: cursor = 1 'pgUp
  29.             Case 80, 77: If cursor < nInputs Then cursor = cursor + 1 'down
  30.             Case 81: cursor = nInputs ' pgDn
  31.             Case 32 TO 122: Inputs$(cursor) = Inputs$(cursor) + k$
  32.         End Select
  33.     End If
  34.     _Limit 30 'up date 30times a sec to save CPU use
  35. 'show results of inputs
  36. For i = 1 To nInputs
  37.     Print i, prompts$(i); "= "; Inputs$(i)
  38.  
  39.  
« Last Edit: June 15, 2021, 10:01:50 am by bplus »