KEY(n): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "'''KEY(n)''' assigns, enables, disables or suspends event trapping of a keypress by setting the flag ON, STOP or OFF. {{PageSyntax}} : KEY({{Parameter|number}}) {ON | OFF | STOP} {{PageDescription}} * Predefined and user defined KEY event number assignments to use with KEY(n): {{WhiteStart}} '''1 to 10'''.............Reserved '''F1 to F10''' function keys only. '''11, 12, 13 and 14'''...Reserved '''Up, Left, Right and Down''' numeric...")
 
No edit summary
Line 7: Line 7:


{{PageDescription}}
{{PageDescription}}
* Predefined and user defined KEY event number assignments to use with KEY(n):  
* Predefined and user defined KEY event number assignments to use with 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 13: Line 13:
     '''30 and 31'''...........Reserved '''F11 and F12''' function keys only.
     '''30 and 31'''...........Reserved '''F11 and F12''' function keys only.
{{WhiteEnd}}
{{WhiteEnd}}
* Keypresses can be read during [[INKEY$]], [[INPUT$]] or [[INPUT]] procedures without losing the input.  
* Keypresses can be read during [[INKEY$]], [[INPUT$]] or [[INPUT]] procedures without losing the input.
* Key event reads will also interrupt [[SLEEP]].  
* Key event reads will also interrupt [[SLEEP]].
* [[KEY(n)]] specific status modes are:
* [[KEY(n)]] specific status modes are:
**'''ON''' enables specific keypress events to be monitored.  
**'''ON''' enables specific keypress events to be monitored.
**'''STOP''' suspends specific keypress reads, but remembers them. When re-enabled the key presses will be returned.
**'''STOP''' suspends specific keypress reads, but remembers them. When re-enabled the key presses will be returned.
**'''OFF''' disables specified keypress reads and will not remember the event.
**'''OFF''' disables specified keypress reads and will not remember the event.
Line 23: Line 23:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' How to trap the LEFT direction keys on both the dedicated cursor keypad and the numeric keypad.
''Example 1:'' How to trap the LEFT direction keys on both the dedicated cursor keypad and the numeric keypad.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|KEY n|KEY}} 15, {{Cl|CHR$}}(128) + {{Cl|CHR$}}(75) ' Assign trap for LEFT arrow key on the cursor keypad
{{Cl|KEY n|KEY}} 15, {{Cl|CHR$}}(128) + {{Cl|CHR$}}(75) ' Assign trap for LEFT arrow key on the cursor keypad
{{Cl|ON KEY(n)|ON KEY(15)}} {{Cl|GOSUB}} CursorPad    
{{Cl|ON KEY(n)|ON KEY(15)}} {{Cl|GOSUB}} CursorPad
{{Cl|KEY(n)|KEY(15)}} {{Cl|ON}} ' enable event trapping                  
{{Cl|KEY(n)|KEY(15)}} {{Cl|ON}} ' enable event trapping


{{Cl|ON KEY(n)|ON KEY(12)}} {{Cl|GOSUB}} NumericPad ' Trap LEFT key on number pad
{{Cl|ON KEY(n)|ON KEY(12)}} {{Cl|GOSUB}} NumericPad ' Trap LEFT key on number pad
{{Cl|KEY(n)|KEY(12)}} {{Cl|ON}} ' enable event trapping                    
{{Cl|KEY(n)|KEY(12)}} {{Cl|ON}} ' enable event trapping


DO
DO
Line 41: Line 41:
NumericPad:
NumericPad:
PRINT "Pressed LEFT key on numeric keypad."
PRINT "Pressed LEFT key on numeric keypad."
RETURN '' ''
RETURN
{{CodeEnd}}
{{CodeEnd}}




''Example 2:'' Trapping the F5 keypress.
''Example 2:'' Trapping the F5 keypress.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|KEY(n)|KEY(5)}} {{Cl|ON}}
{{Cl|KEY(n)|KEY(5)}} {{Cl|ON}}
{{Cl|ON KEY(n)|ON KEY(5)}} {{Cl|GOSUB}} execute
{{Cl|ON KEY(n)|ON KEY(5)}} {{Cl|GOSUB}} execute
Line 57: Line 57:
{{Cl|SLEEP}} 1
{{Cl|SLEEP}} 1
PRINT "Press any key to continue..."
PRINT "Press any key to continue..."
SLEEP '' ''
SLEEP
{{CodeEnd}}
{{CodeEnd}}



Revision as of 01:52, 23 January 2023

KEY(n) assigns, enables, disables or suspends event trapping of a keypress by setting the flag ON, STOP or OFF.


Syntax

KEY(number) {ON | OFF | STOP}


Description

  • Predefined and user defined KEY event number assignments to use with 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

  • Keypresses can be read during INKEY$, INPUT$ or INPUT procedures without losing the input.
  • Key event reads will also interrupt SLEEP.
  • KEY(n) specific status modes are:
    • ON enables specific keypress events to be monitored.
    • STOP suspends specific keypress reads, but remembers them. When re-enabled the key presses will be returned.
    • OFF disables specified keypress reads and will not remember the event.


Examples

Example 1: How to trap the LEFT direction keys on both the dedicated cursor keypad and the numeric keypad.

KEY 15, CHR$(128) + CHR$(75) ' Assign trap for LEFT arrow key on the cursor keypad
ON KEY(15) GOSUB CursorPad
KEY(15) ON ' enable event trapping

ON KEY(12) GOSUB NumericPad ' Trap LEFT key on number pad
KEY(12) ON ' enable event trapping

DO
LOOP UNTIL UCASE$(INKEY$) = "Q" ' Idle loop for demo
SYSTEM

CursorPad:
PRINT "Pressed LEFT key on cursor keypad."
RETURN

NumericPad:
PRINT "Pressed LEFT key on numeric keypad."
RETURN


Example 2: Trapping the F5 keypress.

KEY(5) ON
ON KEY(5) GOSUB execute
PRINT "Press F5 (or ESC) to quit!)"
DO
LOOP UNTIL INKEY$ = CHR$(27) ' idle loop
SYSTEM
execute:
PRINT "You pressed the F5 key..."
SLEEP 1
PRINT "Press any key to continue..."
SLEEP


See also



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