keyboard input

Post Reply
rinwaldmolly
Posts: 16
Joined: Mon Jul 22, 2024 7:29 pm

keyboard input

Post by rinwaldmolly »

Hello!

Refining my first program and wondering how to limit key presses to the options given in an input statement - I'm fine with numbers i.e.

10 input "choose 1,2,3";i$
20 if i$>3 then goto 10
30 if i$=1 goto 40
40 if i$=2 goto 50 etc

But not sure how to do it with alphanumeric options?

10 input "choose 'y' or 'n'";i$

I can see that if it was just a single character I could use:

20 if i$ <> y then got to 10

but not sure how to do it with multiple options.

Any help appreciated!

Jason
User avatar
1024MAK
Posts: 5526
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: keyboard input

Post by 1024MAK »

The first thing to say is that although to the processor, everything is just a number, with high level languages like BASIC, the situation is more complex.

In BASIC, there are numerical values and string values.

Both can be constants or the value can be contained in a variable.

Examples:

10 LET age=78 : REM this puts the numerical value of 78 (a constant) into a numerical variable called age.
20 INPUT x$ : REM this gets some text (a string) and stores the string in the string variable x$. The string can be any character or multiple characters.

In general, you can't mix and match between the two types.

Hence
30 IF i$=1 THEN GO TO 40
won't work.

However, this may:
30 IF i$= "1" THEN GO TO 40
because now you are asking the computer to compare the value in a string variable with a string constant.

Internally, each character is a number. Hence this is also possible:
30 IF i$>="a" AND i$<="z" THEN GO TO 40

However, note that if the string contains more than one character, it may not work as you expect. Also, note that the >= and <= are special characters and are not made up of an individual = or < or > unless you are using the full screen 128 editor.

There is lots of useful information and help in the programming manual. A web page version is here. A PDF version that can be downloaded is here (other copies are available from other sites).

You should also investigate the INKEY$ function.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Spring approaching...
User avatar
XavSnap
Posts: 2193
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: keyboard input

Post by XavSnap »

You should also investigate the INKEY$ function.
Yes, better use the INKEY$...

Code: Select all

10 PRINT "TYPE A KEY 0-9"
20 LET A$=INKEY$
25 IF A$<"0" OR A$>"9" THEN GO TO 20
30 RESTORE 50
31 FOR a=0 TO CODE (A$)-48
32 READ B
33 NEXT a
34 GO TO B
40 REM  ON VALUE GO TO LINE
50 DATA 110,150,200,500,1110,1150,1200,1500,110,2000
2000 PRINT A$,B
2010 PRINT "PRESS (Y)es or (N)o"
2020 IF INKEY$="Y" OR INKEY$="y" THEN GO TO 4000
2030 IF INKEY$="N" OR INKEY$="n" THEN GO TO 5000
2040 GO TO 2020
4000 PRINT "Yes !"
4010 STOP
5000 PRINT "No !"
5010 STOP
In case of INPUT, just test the first character:

Code: Select all

6000 INPUT "PRESS (Y)es or (N)o",a$
6010 IF a$(1)="Y" OR a$(1)="y" THEN GO TO 4000
6020 IF a$(1)="N" OR a$(1)="n" THEN GO TO 5000
6030 GO TO 6000
Note: EO can retrieve a ".BAS" file in text mode, but you had to create a line per command.
":" not supported.

Have fun.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
rinwaldmolly
Posts: 16
Joined: Mon Jul 22, 2024 7:29 pm

Re: keyboard input

Post by rinwaldmolly »

Thanks both - in this instance my program is all driven by input commands hence using variable i for numbers or i$ for letters instead of inkey$ (the above example was typed on the fly not from my listing hence mix up)

Between your two responses I think I can achieve my aim which is to stop the program being interrupted or confused by a key press that is not an option!

I’ve tested for bugs now so if interest I could share the current version as a z80 file?

It was inspired by Steve Vickers simple dice rolling listing and was a perfect chance to experiment with different functions as I worked through the basic manual.

It’s based on the classic fighting fantasy game books and uses the rules system to create a character and can be used for the combat encounters and using potions etc.

The one addition I want to make when I get the hang of array command is to offer an inventory to list items found along the way
Post Reply