Difference between revisions of "ON KEY(n)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 3: Line 3:
  
 
{{PageSyntax}}
 
{{PageSyntax}}
:: ON KEY (n) [[GOSUB]] {linelabel|linenumber}
+
:: ON KEY (n) [{{{KW|GOSUB}} {''linelabel''|''linenumber''}]|[[SUB]]procedure}
  
  
 
''Description:''
 
''Description:''
* The keys apply to the list of keys shown below, where n is the number that represents that key.
+
* Predefined key numbers are shown below, where n is the number that represents that key.
 
 
 
:'''1 - 10''' for '''F1 - F10''' function keys respectively
 
:'''1 - 10''' for '''F1 - F10''' function keys respectively
 
 
:'''11, 12, 13 and 14''' for '''UP, LEFT, RIGHT and DOWN''' numberpad arrows 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]]
 
:'''15-25(29 in QB64)''' for '''user-defined keys'''. See: [[KEY n]], [[Keyboard scancodes]]
 
 
:'''30 - 31''' for '''F11 - F12''' function keys
 
:'''30 - 31''' for '''F11 - F12''' function keys
 +
* [[GOSUB]] with a ''linelabel'' or ''linenumber''  or a [[SUB]] procedure can be used in '''QB64'''. Don't use [[CALL]]!
  
  
''Example:''
+
''Example 1:'' Using ON KEY with [[GOSUB]] to execute code.
 
{{CodeStart}} '' ''
 
{{CodeStart}} '' ''
 
{{Cl|KEY(n)|KEY(1) ON}}
 
{{Cl|KEY(n)|KEY(1) ON}}
Line 29: Line 26:
 
{{Cl|END}}
 
{{Cl|END}}
 
{{Cl|RETURN}} '' ''
 
{{Cl|RETURN}} '' ''
 +
{{CodeEnd}}
 +
 +
 +
''Example 2:'' Using ON KEY with a [[SUB]] procedure to do something.
 +
{{CodeStart}} '' ''
 +
{{Cl|CLS}}
 +
 +
{{Cl|ON KEY(n)|ON KEY}}(10) Control 'action of user defined key press
 +
{{Cl|KEY(n)|KEY}}(10) ON 'turn ON event trapping for key combination
 +
{{Cl|PRINT}} "Press F10 key, escape quits!"
 +
DO: {{Cl|SLEEP}}
 +
  count = count + 1
 +
  {{Cl|PRINT}} count;
 +
  {{Cl|IF...THEN|IF}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|END}} 'escape key exit
 +
{{Cl|LOOP}}
 +
 +
{{Cl|SUB}} Control
 +
{{Cl|PRINT}} "F10 key pressed!";
 +
{{Cl|BEEP}}
 +
{{Cl|END SUB}} '' ''
 
{{CodeEnd}}
 
{{CodeEnd}}
  

Revision as of 17:11, 26 October 2011

The ON KEY(n) statement defines a line number or label to go to when a specified key is pressed.


Syntax

ON KEY (n) [{GOSUB {linelabel|linenumber}]|SUBprocedure}


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
  • GOSUB with a linelabel or linenumber or a SUB procedure can be used in QB64. Don't use CALL!


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: Using ON KEY with a SUB procedure to do something.

CLS ON KEY(10) Control 'action of user defined key press KEY(10) ON 'turn ON event trapping for key combination PRINT "Press F10 key, escape quits!" DO: SLEEP count = count + 1 PRINT count; IF INKEY$ = CHR$(27) THEN END 'escape key exit LOOP SUB Control PRINT "F10 key pressed!"; BEEP END SUB


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page