Difference between revisions of "KEY"

From QB64 Wiki
Jump to navigation Jump to search
(Redirected page to KEY n)
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
The '''KEY''' statement is used to assign a "softkey" string to a key and/or display them.
::::::''Syntax:'' KEY n, string
* KEY LIST displays softkey string values at the bottom of the screen.
* KEY {ON|OFF} turns on or off the softkey display line at bottom of the screen.
* If that function key is pressed the string is returned, n in the syntax is the key to assign the string to.
* '''KEY n,''' defines a "soft key" to monitor and can be any of the following Qbasic predefined values:
::'''1 to 10''' for '''F1 to F10''' function keys only.
::'''11, 12, 13 and 14''' for '''UP, LEFT, RIGHT and DOWN''' arrows respectively on numeric keypad.
::'''15 to 25''' for '''user-defined keys''' using the CHR$(keyflag) value + [[CHR$]]([[Keyboard scancodes]])
::'''30 and 31''' for '''F11 and F12''' function keys only.
<center>'''Assigning function "Soft Keys"'''</center>
* Using '''KEY n''', a Keyboard flag can be one or a combination of any of the following values:
::'''0''' = no keyboard flag (normal typing keys)
::'''1''' = Left Shift key
::'''2''' = Right Shift key
::'''4''' = Ctrl
::'''8''' = Alt
::'''32''' = Num Lock
::'''64''' = Caps Lock
::'''128''' = Extended function keys on a 101 key keyboard (see trapping extended keys below)
:Flag values can be added to monitor more than one key. For example, flag combination 12 would flag both the Ctrl and Alt key presses. Since the flag really determines the key to monitor, you don't necessarily have to use it's scancode. You can look for a key combination such as Ctrl + by using the plus key's scancode which is 13.
<center>KEY 15, CHR$(4) + CHR$(13)</center>
<center>'''Trapping Extended keys (Insert, Home, Page Up, Right Ctrl, R.Alt, and cursor arrow pad)'''</center>
* If you use a 101-key keyboard, you can trap any of the keys on the dedicated cursorpad by assigning the string to any of the keynumber values from 15 to 25 using the 128 keyboard flag:
<center>KEY n, [[CHR$]](128) + [[CHR$]](scancode) ' where n = 15 to 25. See: [[Scancodes]]
KEY 15, CHR$(128) + CHR$(75)  'left arrow  
KEY 16, CHR$(128) + CHR$(72)  'up arrow   
KEY 17, CHR$(128) + CHR$(77)  'right arrow
KEY 18, CHR$(128) + CHR$(80)  'down arrow </center>
<center>Use CHR$(0) for the first byte flag for non-function keys. You can substitute a literal [[STRING]] value to trap also as shown in Example 2.</center>
''Example 1:''
KEY 1, "help"
INPUT "Press F1 or to quit press ENTER: ", a$
Press F1 or to quit press ENTER: helphelphelphelp
:Shows a list of all the string assignments to the function keys F1-F10 (Prints help every time you press F1 in the input)
''Example 2: Trapping the key combination of Control +
{{Cl|KEY}}15, {{Cl|CHR$}}(4) + {{Cl|CHR$}}(13)
{{Cl|ON KEY (n)|ON KEY}}(15) {{Cl|GOSUB}} control
{{Cl|KEY(n)|KEY}}(15) ON
{{Cl|DO}}: {{Cl|SLEEP}}
count = count + 1
{{Cl|PRINT}} count;
{{Cl|IF}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|END}}
{{Cl|PRINT}} "Control key pressed";
''See also:''
[[KEY ON]], [[KEY OFF]]
[[KEY LIST]], [[ON KEY (n)]]

Latest revision as of 22:38, 20 August 2017

Redirect to: