Several times over the years, I've seen people posting and asking questions like, "Why can't I paste text into INPUT?", or, "Why doesn't INPUT accept ALT-number input?"
The answer has always been the same: "It doesn't. You'll just have to write your own input handler to do that!"
So, after ages of telling people they had to write their own, I finally needed to write my own input handler, so I thought I'd share:
_CONTROLCHR OFF
PRINT ExtendedInput$
FUNCTION ExtendedInput$
PCOPY 0, 1
A = _AUTODISPLAY: X = POS(0): Y = CSRLIN
_KEYCLEAR
DO
PCOPY 1, 0
IF _KEYDOWN(100307) OR _KEYDOWN(100308) THEN AltDown = -1 ELSE AltDown = 0
k = _KEYHIT
IF AltDown THEN
SELECT CASE k 'ignore all keypresses except ALT-number presses
CASE 48 TO 57: AltWasDown = -1: alt$ = alt$ + CHR$(k)
END SELECT
ELSE
SELECT CASE k 'without alt, add any keypresses to our input
CASE 8: oldin$ = in$: in$ = LEFT$(in$, LEN(in$) - 1) 'backspace to erase input
CASE 9: oldin$ = in$: in$ = in$ + SPACE$(4) 'four spaces for any TAB entered
CASE 32 TO 128
IF _KEYDOWN(100305) OR _KEYDOWN(100306) THEN
IF k = 118 OR k = 86 THEN oldin$ = in$: in$ = in$ + _CLIPBOARD$ 'ctrl-v paste
IF k = 122 OR k = 90 THEN SWAP in$, oldin$ 'ctrl-z undo
ELSE
oldin$ = in$: in$ = in$ + CHR$(k) 'add input to our string
END IF
END SELECT
END IF
alt$ = RIGHT$(alt$, 3)
IF AltWasDown = -1 AND AltDown = 0 THEN
v = VAL(alt$)
IF v >= 0 AND v <= 255 THEN in$ = in$ + CHR$(v)
alt$ = "": AltWasDown = 0
END IF
blink = (blink + 1) MOD 30
LOCATE Y, X: IF blink \ 15 THEN PRINT in$ ELSE PRINT in$ + "_"
_DISPLAY
_LIMIT 30
LOOP UNTIL k = 13
PCOPY 1, 0
LOCATE Y, X: PRINT in$
ExtendedInput$ = in$
IF A THEN _AUTODISPLAY
END FUNCTION
This is used as basically a replacement for INPUT$, and handles quite a few special cases for us.
- We handle regular input, as normal.
- CTRL-V works as a paste command, so we can paste clipboard text into the input.
- CTRL-Z works as an undo, to undo the last change to our text.
- ALT-number keys can be used to enter an ASCII character into the input.
- TAB inserts four spaces into our input automatically for ease of formatting.
Like INPUT, this pauses program execution until we finish our input, and it plays nicely with background processes.
Try it out and play around with it. See if it performs as you'd expect/want it to. Type some junk in it. Then hold down ALT and type in a few numeric codes. (ALT-65 is "A", ALT-1 is the little smiley face, for example.) Try CTRL-V to paste something, and CTRL-Z to undo your last input (which can be quite useful if you paste the wrong junk into your text with CTRL-V).
I think this should be easily expandable so people could customize it to suit their own specific needs, if they have any which is different from what it's currently doing. At the moment, it's doing all that I'd expect it to do, for me, and I hope others might find it useful as well.
At least now, when somebody asks about "Why can't INPUT do XXXX" again in the future, I can at least say, "Take a look at this, see if it suits your needs, and if not, maybe it'll be a nice starting point to help you write your own input handler." :D