ON KEY(n): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The ON KEY(n) statement defines a line number or label to go to (or a SUB to run) when a specified key is pressed. {{PageSyntax}} : ON KEY(n) GOSUB {{Parameter|linelabel}}|{{Parameter|linenumber}} : ON KEY(n) SUBprocedure {{PageDescription}} * Predefined and user defined KEY event number assignments to use with ON KEY(n): {{WhiteStart}} '''1 to 10'''.............Reserved '''F1 to F10''' function keys only. '''11, 12, 13 and 14'''...R...")
 
No edit summary
Line 8: Line 8:


{{PageDescription}}
{{PageDescription}}
* Predefined and user defined KEY event number assignments to use with ON KEY(n):  
* Predefined and user defined KEY event number assignments to use with ON KEY(n):
{{WhiteStart}}    '''1 to 10'''.............Reserved '''F1 to F10''' function keys only.
{{WhiteStart}}    '''1 to 10'''.............Reserved '''F1 to F10''' function keys only.
     '''11, 12, 13 and 14'''...Reserved '''Up, Left, Right and Down''' numeric keypad arrows only
     '''11, 12, 13 and 14'''...Reserved '''Up, Left, Right and Down''' numeric keypad arrows only
Line 20: Line 20:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Using ON KEY with [[GOSUB]] to execute code.
''Example 1:'' Using ON KEY with [[GOSUB]] to execute code.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|KEY(n)|KEY(1) ON}}
{{Cl|KEY(n)|KEY(1) ON}}
{{Cl|ON KEY(n)|ON KEY}}(1) {{Cl|GOSUB}} trap
{{Cl|ON KEY(n)|ON KEY}}(1) {{Cl|GOSUB}} trap
Line 29: Line 29:
{{Cl|PRINT}} "You pressed F1 like I told you to :)"
{{Cl|PRINT}} "You pressed F1 like I told you to :)"
{{Cl|END}}
{{Cl|END}}
{{Cl|RETURN}} '' ''
{{Cl|RETURN}}
{{CodeEnd}}
{{CodeEnd}}




''Example 2:'' Setting multiple ON KEY statements to send different values to a [[SUB]] procedure.
''Example 2:'' Setting multiple ON KEY statements to send different values to a [[SUB]] procedure.
{{CodeStart}} '' ''
{{CodeStart}}
 
 
{{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} 10
{{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} 10
   {{Cl|KEY n|KEY}} n, {{Cl|STR$}}(n)  '  assigns soft key as a numerical string  
   {{Cl|KEY n|KEY}} n, {{Cl|STR$}}(n)  '  assigns soft key as a numerical string
   {{Cl|ON KEY(n)|ON KEY}}(n) Funct n  'designate SUB procedure and parameter value passed
   {{Cl|ON KEY(n)|ON KEY}}(n) Funct n  'designate SUB procedure and parameter value passed
   {{Cl|KEY(n)|KEY}}(n) ON '        turns each key event monitor on
   {{Cl|KEY(n)|KEY}}(n) ON '        turns each key event monitor on
Line 50: Line 50:
{{Cl|CLS}}'                  clears the screen and refreshes bottom soft key list
{{Cl|CLS}}'                  clears the screen and refreshes bottom soft key list
{{Cl|PRINT}} "You pressed F"; {{Cl|LTRIM$}}({{Cl|STR$}}(num%))
{{Cl|PRINT}} "You pressed F"; {{Cl|LTRIM$}}({{Cl|STR$}}(num%))
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}



Revision as of 02:13, 23 January 2023

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


Syntax

ON KEY(n) GOSUB linelabel|linenumber
ON KEY(n) SUBprocedure


Description

  • Predefined and user defined KEY event number assignments to use with ON KEY(n):

Template:WhiteStart 1 to 10.............Reserved F1 to F10 function keys only.

    11, 12, 13 and 14...Reserved Up, Left, Right and Down numeric keypad arrows only
    15 to 29............user-defined keys using value: CHR$(keyflag) + CHR$(scancode)
    30 and 31...........Reserved F11 and F12 function keys only.

Template:WhiteEnd

  • See the KEY n page for user defined key or key combination presses and F function softkey assignments.
  • GOSUB with a linelabel or linenumber or a SUB procedure (without the CALL keyword) can be triggered in QB64.


Examples

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
  KEY n, STR$(n)  '   assigns soft key as a numerical string
  ON KEY(n) Funct n  'designate SUB procedure and parameter value passed
  KEY(n) ON '         turns each key event monitor on
NEXT
KEY ON  'displays F1 to F10 soft key assignments at bottom of screen

DO
LOOP UNTIL INKEY$ = CHR$(27)
END

SUB Funct (num%)
CLS'                  clears the screen and refreshes bottom soft key list
PRINT "You pressed F"; LTRIM$(STR$(num%))
END SUB


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link