Difference between revisions of "ON KEY(n)"
Jump to navigation
Jump to search
Navigation:
imported>Clippy m |
imported>Clippy m |
||
Line 29: | Line 29: | ||
− | ''Example 2:'' | + | ''Example 2:'' Setting multiple ON KEY statements to send different values to a [[SUB]] procedure. |
{{CodeStart}} '' '' | {{CodeStart}} '' '' | ||
− | {{Cl| | + | {{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} 10 |
+ | {{Cl|ON KEY(n)|ON KEY}}(n) Funct n | ||
+ | {{Cl|KEY(n)|KEY}}(n) ON | ||
+ | {{Cl|NEXT}} | ||
− | + | DO | |
− | |||
− | |||
− | DO | ||
− | |||
− | |||
− | |||
− | |||
− | {{Cl|SUB}} | + | {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) |
− | {{Cl|PRINT}} " | + | {{Cl|END}} |
− | {{Cl| | + | |
+ | {{Cl|SUB}} Funct (num%) | ||
+ | {{Cl|PRINT}} "You pressed F"; {{Cl|LTRIM$}}({{Cl|STR$}}(num%)) | ||
{{Cl|END SUB}} '' '' | {{Cl|END SUB}} '' '' | ||
{{CodeEnd}} | {{CodeEnd}} |
Revision as of 22:35, 26 October 2011
The ON KEY(n) statement defines a line number or label to go to when a specified key is pressed.
Syntax
Description:
- Predefined key numbers are shown below, where n is the number that represents that key.
- 1 - 10 for F1 - F10 function keys respectively
- 11, 12, 13 and 14 for UP, LEFT, RIGHT and DOWN numberpad arrows respectively.
- 15-25(29 in QB64) for user-defined keys. See: KEY n, Keyboard scancodes
- 30 - 31 for F11 - F12 function keys
Example 1: Using ON KEY with GOSUB to execute code.
KEY(1) ON ON KEY(1) GOSUB trap PRINT "Press F1 to quit!" DO:LOOP 'never ending loop trap: PRINT "You pressed F1 like I told you to :)" END RETURN
Example 2: Setting multiple ON KEY statements to send different values to a SUB procedure.
FOR n = 1 TO 10 ON KEY(n) Funct n KEY(n) ON NEXT DO LOOP UNTIL INKEY$ = CHR$(27) END SUB Funct (num%) PRINT "You pressed F"; LTRIM$(STR$(num%)) END SUB
See also: