Q(uick)BASIC Statement (Assignment): KEY
Quick View
KEY
A device I/O statement that assigns soft-key string values to function keys and then can be used to display the soft-key values in various ways
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- KEY key%, stringexpression$
- KEY LIST
- KEY ON
- KEY OFF
Description/Parameter(s)
key% | The number of a function key. Use 1 through 10 for function keys F1 through F10. Use 30 and 31 for function keys F11 and F12 on extended keyboards. |
stringexpression$ | A string of up to 15 characters that is returned when the function key is pressed. |
LIST | Displays the assignments for each key. |
ON | Turns on the function-key display line. |
OFF | Turns off the function-key display line. |
Example
KEY 4, "MENU" + CHR$ (13)
KEY LIST
KEY 4, ""
KEY LIST
See also:
Description/Parameter(s)
Syntax | Use |
KEY n, stringexpression | Assigns soft-key string values |
KEY LIST | Displays soft-key string values |
KEY {ON|OFF} | Turns on and off the soft-key display line at the bottom of the screen |
Assigning Strings to Function Keys
- The placeholder n is a number representing the FUNCTION key. The values for n are 1 to 10 for the FUNCTION keys, and 30 and 31 for FUNCTION keys F11 and F12 on 101-key keyboards. The stringexpression is a string of up to 15 characters that is returned when the function key is pressed. If the stringexpression is longer than 15 characters, the extra characters are ignored.
- The KEY statement allows you to designate special "soft-key" functions--strings that are returned when FUNCTION keys are pressed.
- Assigning a null string to a soft key disables the FUNCTION key as a soft key.
- If the FUNCTION key number is not in the correct range, an error message is displayed that reads "Illegal function call," and the previous key string expression is retained.
- Displaying Soft Key Assignments
- You may display soft keys with the KEY ON, KEY OFF, and KEY LIST statements:
Statement | Action |
KEY ON | Displays the first six characters of the soft-key string values on the bottom line of the screen. |
KEY OFF | Erases the soft-key display from the bottom line, making that line available for program use. It does not disable the FUNCTION keys. |
KEY LIST | Displays all soft-key values on the screen, with all 15 characters of each key displayed. |
- If a soft key is pressed, the effect is the same as if the user typed the string associated with the soft key. INPUT$, INPUT, and INKEY$ can all be used to read the string produced by pressing the soft key.
Examples
Here is an example of assigning and disabling a soft key. KEY LIST displays key values after KEY 4 has been assigned and again after it has been disabled:
CLS ' Clear screen
KEY 4, "MENU" + CHR$(13) 'Assigns to soft key 4 the string
'"MENU" followed by a carriage return.
KEY LIST
KEY 4, "" 'Disables soft key 4.
KEY LIST
Here is an example of using KEY statements to set up one-key equivalents of menu selections. For example, pressing F1 is the same as entering the string "Add":
CLS ' Clear screen
DIM KeyText$(3)
DATA Add, Delete, Quit
' Assign soft-key strings to F1 to F3.
FOR I = 1 TO 3
READ KeyText$(I)
KEY I, KeyText$(I) + CHR$(13)
NEXT I
' Print menu.
PRINT " Main Menu" : PRINT
PRINT " Add to list (F1)"
PRINT " Delete from list (F2)"
PRINT " Quit (F3)" : PRINT
' Get input and respond.
DO
LOCATE 7,1 : PRINT SPACE$(50);
LOCATE 7,1 : INPUT " Enter your choice:", R$
SELECT CASE R$
CASE "Add", "Delete"
LOCATE 10,1 : PRINT SPACE$(15);
LOCATE 10,1 : PRINT R$;
CASE "Quit"
EXIT DO
CASE ELSE
LOCATE 10,1 : PRINT "Enter first word or press key."
END SELECT
LOOP
Syntax
- KEY n%, stringexpression$
- KEY ON
- KEY OFF
- KEY LIST
Description/Parameter(s)
- KEY n%, stringexpression$ assigns soft-key string values. The argument n% is a number that represents the function key. The values for n% are 1 to 10 for the function keys, and 30 and 31 for function keys F11 and F12 on 101-key keyboards. The argument stringexpr$ is a string of up to 15 characters that is returned when the function key is pressed. If the string is longer than 15 characters, the extra characters are ignored.
Usage Notes
- The KEY statement allows you to designate special "soft-key" functions - strings that are returned when function keys are pressed.
- Assigning a null string to a soft key disables the function key as a soft key.
- If the function key number is not in the correct range, BASIC generates the error message, "Illegal function call," and the previous key string expression is retained.
- You may display soft keys with the KEY ON, KEY OFF, and KEY LIST statements:
Statement | Action |
KEY ON | Displays the first five to six characters of the soft-key string values on the bottom line of the screen. |
KEY OFF | Erases the soft-key display from the bottom line, making that line available for program use. It does not disable the function keys. |
KEY LIST | Displays all soft-key values on the screen, with all 15 characters of each key displayed. |
- If a soft key is pressed, the effect is the same as if the user typed the string associated with the soft key.
- INPUT$, INPUT, and INKEY$ all can be used to read the string produced by pressing the soft key.
Examples
This is an example of assigning and disabling a soft key. KEY LIST displays key values after soft key 4 has been assigned and again after it has been disabled.
CLS 'Clear screen.
KEY 4, "MENU" + CHR$(13) 'Assign to soft key 4 the string "MENU"
'followed by a carriage return.
KEY LIST
KEY 4, "" 'Disable soft key 4.
KEY LIST
This example uses the KEY statement to set up one-key equivalents of menu selections. For example, pressing F1 is the same as entering the string "Add".
CLS 'Clear screen.
DIM KeyText$(3)
DATA Add, Delete, Quit
'Assign soft-key strings to F1 to F3.
FOR I = 1 TO 3
READ KeyText$(I)
KEY I, KeyText$(I) + CHR$(13)
NEXT I
'Print menu.
PRINT " Main Menu": PRINT
PRINT " Add to list (F1)"
PRINT " Delete from list (F2)"
PRINT " Quit (F3)": PRINT
'Get input and respond.
DO
LOCATE 7, 1: PRINT SPACE$(50);
LOCATE 7, 1: INPUT " Enter your choice:", R$
SELECT CASE R$
CASE "Add", "Delete"
LOCATE 10, 1: PRINT SPACE$(15);
LOCATE 10, 1: PRINT R$;
CASE "Quit"
EXIT DO
CASE ELSE
LOCATE 10, 1: PRINT "Enter first word or press key."
END SELECT
LOOP