The standard INPUT function provided by Basic is easy to use, but has a number of limitations. For example, the limit on user input is 238 characters, so if the user holds down a key, even the best laid out screen will be spoiled. There is also no restriction on which keyboard characters are accepted, the mouse is completely ignored, and so on.
The function supplied here solves these problems, and a few more besides. It takes six parameters, as you can see from the definition. The first is the maximum length of string that the user may supply. The second and third define the range of ASCII characters that the routine will accept. The fourth is a string containing any characters within the above range which should be ignored (use "" if there are none). The fifth parameter is the so-called "paint" character which will mark the position of each character of the input string before it is entered (e.g. "-" will cause a row of dashes to be used, or " " will give a blank space). Finally the sixth parameter indicates whether the input should be echoed to the screen (TRUE) or not (FALSE). You might set this to FALSE on a password input to avoid the password appearing on the screen.
The function returns the string input by the user. The user must terminate his input either by pressing the Return key, or the Select button. If on the other hand he uses the Menu or Adjust buttons at any time, this will immediately cancel his input, and to signify this the string returned by the function will be the single character CHR$127 (ASCII delete).
If the user terminates the string using the mouse, the variable exitchoice% will be set to the mouse button number. This enables you to distinguish between cancelling with the Menu and the Adjust button, which may be important for some applications.
If you run the program you can see the input function in use. In the example the length of input is restricted to 10 characters (as it might be if used for inputting filenames), and certain characters, including brackets, colons and so on, are excluded.
You will see that the value returned by the function (line 130) is checked for validity before the result is used. Also the variable exitchoice% is checked if the user cancelled his input. If he used Adjust then a further input is sought, while if he cancelled with the Menu button, the program terminates.
Finally, although the function is designed primarily for the input of text strings, it is relatively easy to customise. To make it yield a numeric result, use:
result=VAL(FNinput(len%,45,69,"/:;<=>?@ABCD","-",TRUE))
For 4-digit hex input, use:
result=EVAL("&"+FNinput(4,48,70,":;<=>?@","-",TRUE))
10 REM >Prc4-1-3
20 REM Procedure/Function Library
30 REM Version A 0.3
40 REM Author Lee Calcraft
50 REM RISC User July/Aug 1989
60 REM Copyright Lee Calcraft
70 :
80 MODE12
90 PRINTTAB(10,7)"INPUT FUNCTION TEST"
100 REPEAT
110 PRINTTAB(0,10)"New filename please
";
120 result$=FNinput(10,48,122,":;<=>?@[\]^£",".",TRUE)
130 IF result$<>CHR$127 THEN
140 PRINT'"Valid input: ";result$;SPC(10)
150 ELSE VDU7:PRINT'"Cancelled entry";SPC(10)
160 ENDIF
170 UNTIL exitchoice%=2
180 END
190 :=================================
200 REM Input function - Returns CHR$ 127 if input invalid
210 REM exitchoice% holds mouse button
at exit
220 :
230 DEFFNinput(len%,lochr%,hichr%,exclude$,paint$,echo%)
240 LOCAL n%,paint%,inputok%,quitinput%
250 quitinput%=FALSE:inputok%=FALSE
260 input$="":n%=0:exitchoice%=0
270 paint%=ASC(paint$)
280 PRINT STRING$(len%,paint$);STRING$(len%,CHR$8);
290 REPEAT
300 PROCmousewait(-24)
310 IF z%>0 THEN
320 IF z%=4 THEN inputok%=TRUE ELSE quitinput%=TRUE
330 ELSE
340 CASE TRUE OF
350 WHEN w%=13:inputok%=TRUE
360 WHEN w%=127 AND n%>0:n%-=1
370 input$=LEFT$(input$,n%):VDU8,paint%,8
380 WHEN w%>=lochr% AND w%<=hichr% AND
INSTR(exclude$,CHR$w%)=0
390 IF n%>=len% THEN
400 VDU7
410 ELSE input$+=CHR$(w%):n%+=1
420 IF echo% THEN VDUw% ELSE VDU9
430 ENDIF
440 ENDCASE
450 ENDIF
460 UNTIL quitinput% OR inputok%
470 IF quitinput% THEN input$=CHR$127
480 exitchoice%=z%:PRINT:
490 PROCmousewait(0)
500 =input$
510 :=================================
520 DEFPROCmousewait(n)
530 REM 0-7: Wait for this mouse state
540 REM neg: Wait for zero first
550 REM 8: Wait for any button
560 REM 16: Wait for keypress
570 LOCAL anypress,inkey,m
580 *FX21,9
590 IF n<0 THEN PROCmousewait(0):n=ABS(n)
600 IF (n AND 16)>0 THEN inkey=TRUE:*FX21
610 IF (n AND 8)>0 THEN anypress=TRUE
620 m=n AND 7:w%=-1
630 REPEAT
640 MOUSE x%,y%,z%
650 IF inkey THEN w%=INKEY(0)
660 UNTIL (z%=m AND NOT (n>7 AND m=0))
OR (anypress AND (z%>0)) OR (inkey AND (w%>-1))
670 ENDPROC