RISCOS.com

www.riscos.com Technical Support:
BBC BASIC Reference Manual

 


Inputting data


This chapter describes several methods by which you can input data into your BASIC program:

  • from the keyboard
  • from predefined data within your program
  • by programming keys on the keyboard
  • from a mouse.

Inputting data from the keyboard

There are three commands you can use to input data from the keyboard:

  • The INPUT command allows a program to request information.
  • The GET command waits for the user to press a single key.
  • The INKEY command waits a specified length of time for the user to press a single key.

Note that you are advised not to use these three commands in BASIC programs written under the window manager environment (see the Window managed programs section).

INPUT

The INPUT statement allows a program to request information from the user.

The following program gives an example:

10 PRINT "Give me a number and I'll double it";
20 INPUT X
30 PRINT "Twice ";X " is ";X*2

When you run this program, the INPUT command on line 20 displays a question mark on the screen and waits for you to enter data. The number you type is assigned to the variable X. If you do not type anything, or type letters or symbols instead, X is assigned the value 0.

INPUT may also be used with string and integer variables:

10 PRINT "What is your name ";
20 INPUT A$
30 PRINT "Hello ";A$

Line 10 in each of the above two programs is used to print a message on the screen indicating the type of response required. The INPUT statement allows text prompts to be included, so the program above could be written more neatly as:

10 INPUT "What is your name ",A$
20 PRINT "Hello ";A$

The comma in line 10 tells the computer to print a question mark when it wants input from the keyboard. If you leave out the comma, the question mark is not printed. A semi-colon may be used, with exactly the same effect as the comma.

When the program is being executed, the INPUT statement requires you to press Return if you wish to send what you have typed to the computer. Until you press Return, you can delete all or part of what you have typed by pressing Delete or Ctrl-U to erase the whole line.

When you are inputting a string, the computer ignores any leading spaces and anything after a comma, unless you put the whole string inside quotation marks.

To input a whole line of text, including commas and leading spaces, INPUT LINE (or LINE INPUT) may be used:

10 INPUT A$
20 INPUT LINE B$
30 PRINT A$
40 PRINT B$

RUN the above program and, in response to each of the question marks, type

Hello, how are you?

This produces the following output:

Hello
Hello, how are you?

Several inputs may be requested at one time:

10 INPUT A,B,C$

You may enter the data individually, pressing Return after each item. In this case you are prompted with a question mark until you enter the number required. Alternatively, you can give all the inputs on one line, separated by commas.

GET and GET$

Single-character input may be used to read a single key press:

10 PRINT "Press a key"
20 A$ = GET$
30 PRINT "The key you pressed was ";A$

In this example the program waits at line 20 until you press a key. As soon as you do so, the character that key represents is placed in A$. You do not have to press Return and so do not get the chance to change your mind.

GET is similar to GET$ but returns the ASCII code of the key pressed, instead of the character.

INKEY and INKEY$

INKEY$ is similar to GET$, except that it does not wait indefinitely for a key to be pressed. You give it a time limit and it waits for that length of time only (unless a key is pressed first). For example:

10 PRINT "You have 2 seconds to press a key"
20 A$ = INKEY$(200)

The number following the INKEY$ is the number of hundredths of a second it waits. If a key is pressed in time, A$ holds the character which was typed. Otherwise, A$ is the null string.

INKEY is used in a similar manner to INKEY$: it waits for a given time for a key to be pressed, and then returns the ASCII code for the key pressed, or -1 if no key is pressed within this time.

Including data as part of a program

Predefined data may be included within a program and saved as part of it. When the program is run, individual items of data are read and assigned to variables as follows:

10 FOR I% = 1 TO 4
20 READ age%, dog$
30 PRINT "Name: ";dog$ " Age: ";age%
40 NEXT I%
50 DATA 9,"Laddie",3,"Watson"
60 DATA 1
70 DATA "Mungo",3,"Honey"

You may use as many DATA statements as you like, but you must make sure that the type of each item of data matches the type of the variable into which it is being read. Each DATA statement can be followed by one or more items of data separated by commas.

You can usually leave out the quotation marks around strings, but they are needed if you want to include spaces or commas in the string.

For example,

10 DATA Hello, my name is
20 DATA Marvin
30 READ A$,B$
40 PRINT A$;B$

produces:

Hellomy name is

To obtain the sentence Hello, my name is Marvin, change the program as follows:

10 DATA "Hello, my name is"
20 DATA " Marvin"
30 READ A$,B$
40 PRINT A$;B$

A DATA statement must appear as the first statement on a line, otherwise it will not be found. If BASIC encounters a DATA statement while executing a program, it ignores it and goes on to the next line.

When it attempts to READ the first item of data, it scans through the lines of the program from the start until it finds the first DATA statement and uses the first item of data on this line. The next READ uses the second item and so on until the DATA statement has no more items left, at which point the next DATA statement is searched for and used.

If there is insufficient data, the computer produces an error message, such as:

Out of data at line 20

This indicates that it has tried to READ an item of data, but that all items have already been read.

You might have a lot of different sections of DATA, and want to start reading from a certain point. You can do this using the RESTORE statement. It is followed by a line number. BASIC will start subsequent searches for DATA from that line instead of from the start of the program. For example, the program below

10 RESTORE 60
20 READ A$
30 PRINT A$
40 END
50 DATA First line of data
60 DATA Second line of data

will print out

Second line of data

because the RESTORE causes BASIC to start the search for DATA statements at line 60.

Because line numbers can't be used in procedure libraries, a special form of RESTORE is provided so that you can still include data in them. If you say RESTORE +offset, BASIC will start searching for DATA statements at offset+1 lines from where the RESTORE statement is located. For example, if you had the following lines:

1000 RESTORE +0
1010 DATA ...
1020 DATA ...

the next READ would read data from line 1010. If line 1000 was RESTORE +1, then data would be read next from line 1020, and so on.

A further useful feature is the ability to remember where data is currently being read from (LOCAL DATA), read data from another part of the program, then restore the original place (RESTORE DATA). This is mainly useful in functions and procedures, so is explained in the section dealing with them.

A note about line numbers. In general, if you use line numbers anywhere in a program (and there should be very few situations where you have to), they should be simple numbers in the range 0 to 65279, not expressions like start%+10*n%. Otherwise, if the program is renumbered, it will stop working since BASIC does not know how to change the expression in the right way.

Programming the keyboard

Waiting for input

A program can wait for a key to be pressed, either indefinitely using GET and GET$, or for a defined length of time using INKEY and INKEY$. Normally, every time you press a key, it is placed in the keyboard buffer which is a temporary block of memory used to store key presses until BASIC is ready to read them. Up to 31 key presses may be typed ahead like this.

The GET and GET$ instructions look in the keyboard buffer for a key. Hence they take note of keys which were pressed before the input instructions were executed. If, for instance, you want to ensure that you only read keys pressed after a prompt has been displayed, you can empty or flush the buffer before using these instructions. Then you can be sure that the key obtained is in response to the prompt and not just an accidental press of the keyboard a few moments before. To do this, use the operating system command:

*FX 15,1
Using the Tab & cursor keys to get ASCII code

The cursor editing keys can be made to generate ASCII codes when they are pressed, rather than performing their normal cursor editing functions, by typing

*FX 4,1

The codes they return are:

Key Code
Copy 135
<- 136
-> 137
[DOWN] 138
[UP] 139

You can restore cursor copying by giving the command

*FX 4

The Tab key can be made to return any ASCII value you choose by typing

*FX 219,n

where n is the ASCII code you want it to return.

The following program uses these features to move a block around the screen until Copy is pressed, and then to leave it at its current location. Don't worry if you don't understand all of the statements (e.g. RECTANGLE and REPEAT); they are all described later on in the manual.

Note that this method of redefining keys to generate ASCII codes is not compatible with BASIC programs written under the window manager environment (described in the Window managed programs section).

 10 MODE 1
 20 *FX 4,1
 30 x = 600 : y = 492
 40 oldx = x : oldy = y
 50 RECTANGLE FILL x,y,80,40
 60 REPEAT
 70   *FX 15,1
 80   key = GET
 90   CASE key OF
100     WHEN 135 : END
110     WHEN 136 : x -= 20
120     WHEN 137 : x += 20
130     WHEN 138 : y -= 20
140     WHEN 139 : y += 20
150   ENDCASE
160   RECTANGLE FILL oldx,oldy,80,40 TO x,y
170   oldx = x : oldy = y
180 UNTIL FALSE
Scanning the keyboard

When you give INKEY a positive parameter, it waits for a given length of time for a particular key to be pressed; but it has an additional function. If you give INKEY a negative parameter it tests to see if a particular key is pressed at that instant.

This feature is particularly useful for real-time applications where the computer is constantly reacting to the current input it is being given, rather than stopping and waiting for you to decide what to do next. For example:

210 IF INKEY(-66) THEN PRINT "You were pressing A"

Another advantage is that it lets you check for keys like Shift and Ctrl being pressed, which you cannot do with the other input functions.

The list of negative values associated with each of the keys is given in Appendix D: INKEY values.

Using the mouse in programs

The mouse provides a convenient method of supplying information to a program. This information is in three parts:

  • a position on the screen
  • details of which of the buttons are currently being pressed
  • the time of the last mouse 'event'.

To input this information, type

MOUSE x,y,buttons,when

The values returned in x and y give the position of the mouse. The variable buttons gives details of the mouse buttons currently pressed. Finally, when gives the value of a centi-second timer. This timer starts at 0 when the machine is switched on. So, when gives the last time a mouse button was pressed or released, or the current time if no presses or releases are 'pending'. You can omit the last comma and variable if you are not interested in the time.

The buttons variable has a value whose meaning is as follows:

Buttons Details
0 No buttons pressed
1 Adjust (righthand) only pressed
2 Menu (middle) only pressed
3 Adjust and Menu pressed
4 Select (lefthand) only pressed
5 Select and Adjust pressed
6 Select and Menu pressed
7 All three buttons pressed
Linking the mouse to a pointer

The following program is a very simple sketchpad program which draws lines as you move the mouse around and hold down its buttons:

10 MODE 12
20 MOVE 0,0
30 REPEAT
40   MOUSE x,y,button
50   GCOL button + 1
40   DRAW x,y
50 UNTIL FALSE

In order to be able to see the position of the mouse on the screen, it can be linked to a pointer. The easiest way to show the mouse pointer is to use the BASIC statement MOUSE ON. This gives the pointer an arrow shape and displays it on the screen. To turn the pointer off, use MOUSE OFF.

Now, whenever you move the mouse, the pointer moves with it on the screen indicating its current position. This enables the sketchpad program shown above to be altered so that you can move to the position you want and then draw a line to this new position by pressing any button:

5 MODE 15
10 MOUSE ON
20 MOVE 0,0
40 REPEAT
50   REPEAT
60     MOUSE x,y,button%
70   UNTIL button% <> 0
80   DRAW x,y
90 UNTIL FALSE

For more details about the MOUSE statement see the chapter entitled Keywords.

Programming function keys

The keys across the top of the keyboard labelled F1 to F12 are function keys. These can be programmed so that they generate any string you like when they are pressed. For example, type

*KEY1 "*CAT"

Now when you press F1 the string *CAT is printed on the screen as though you had typed it.

Try changing the definition to:

*KEY1 "*CAT |M"

The | sign means that the character following it is to be interpreted as a control character. In this case it is a Ctrl-M which is being included in the string. This performs the same function as pressing Return. A full list of the control characters is given in Appendix G: VDU Commands.

Now when you press F1, the string *CAT is printed and Return is 'pressed' automatically so the current directory is catalogued immediately.

Storing a series of commands

A whole series of commands can be stored in one key. The following defines a key to select screen mode 3 and list the current program in paged mode.

*KEY2 "MODE 3 |M |N LIST |M"
Storing a small BASIC program

You can even define a key so that it contains a small BASIC program:

*KEY 3 "10 MODE 15 |M 20 FOR I% = 1 TO 100|M 30 CIRCLE RND(1279), RND(1024), 50 + RND (300) |M 40 N. |M RUN |M"

The quotation marks around the string are not strictly necessary. However, it is important to remember that everything on the line after the *KEY command is treated as part of the string. So if *KEY is used in a program, it must be the last statement on the line.

Using other keys as additional function keys

The key labelled PRINT acts as function key 0. In addition, the cursor editing keys and Copy can be made to behave as function keys 11 to 15 by giving the command:

*FX 4,2

Following this command, the keys, instead of having their normal cursor editing effects, return the function key strings assigned to them:

Key *KEY number
Copy 11
<- 12
-> 13
[DOWN] 14
[UP] 15

To return them to their normal state, type *FX 4

Symbols in function key strings

The following special characters are allowed in function key strings:

|| means |
|!ch means the following character code + 128
!? means Delete (i.e. CHR$(127))
|" means " (useful for making " the first character)
<n> means CHR$n

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015