{{QBDLDATE:05-20-2022}} {{QBDLTIME:23:13:57}} The [[KEY n]] statement is used to assign a "soft key" string or a flag and scan code to a function key or display function soft key assignments. {{PageSyntax}} : '''KEY ''n%'', ''textString$''''' : '''KEY ''n%'', CHR$(''keyFlag%'') + CHR$(''scanCode'')''' ==Function Soft Key Strings (1 to 10, 30 & 31)==
([[#toc|Return to Table of Contents]])
==Examples== ''Example 1:'' Shows a list of all the string assignments to the function keys F1-F12 (Prints help every time F1 is pressed in the input) {{CodeStart}} {{Cl|KEY n|KEY}} 1, "Help" {{Cl|KEY LIST}} INPUT "Press F1 or to quit press ENTER: ", a$ {{CodeEnd}} {{OutputStart}} F1 Help F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Press F1 or to quit press ENTER: HelpHelpHelpHelp {{OutputEnd}} ''Example 2:'' Trapping the Control + key combination. Use the Control Keyboard flag 4 and + key scancode 13. {{CodeStart}} {{Cl|CLS}} {{Cl|KEY n|KEY}} 15, {{Cl|CHR$}}(4) + {{Cl|CHR$}}(13) 'scancode for "=" or "+" key is 13 {{Cl|ON KEY (n)|ON KEY}}(15) {{Cl|GOSUB}} control 'action of user defined key press {{Cl|KEY(n)|KEY}}(15) ON 'turn ON event trapping for key combination {{Cl|PRINT}} "Press Ctrl and plus key combination, escape quits!" {{Cl|DO}}: {{Cl|SLEEP}} count = count + 1 {{Cl|PRINT}} count; {{Cl|IF}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|END}} 'escape key exit {{Cl|LOOP}} control: 'NUMBER LOCK MUST BE OFF! {{Cl|PRINT}} "Control and + keys pressed!"; {{Cl|RETURN}} {{CodeEnd}} {{small|Code by Ted Weissgerber}} ''Example 3:'' Differentiating the extended cursor keypad arrows from the predefined Number Pad arrow keys. {{CodeStart}} 'predefined keys 11 to 14 for number pad arrows only {{Cl|ON KEY(n)|ON KEY}}(11) {{Cl|GOSUB}} UpNum: {{Cl|KEY(n)|KEY}}(11) ON 'up {{Cl|ON KEY(n)|ON KEY}}(12) {{Cl|GOSUB}} LNum: {{Cl|KEY(n)|KEY}}(12) ON 'left {{Cl|ON KEY(n)|ON KEY}}(13) {{Cl|GOSUB}} RNum: {{Cl|KEY(n)|KEY}}(13) ON 'right {{Cl|ON KEY(n)|ON KEY}}(14) {{Cl|GOSUB}} DnNum: {{Cl|KEY(n)|KEY}}(14) ON 'down 'user defined keys use extended key flag 128 + scan code {{Cl|ON KEY(n)|ON KEY}}(15) {{Cl|GOSUB}} UpPad {{Cl|KEY n|KEY}} 15, {{Cl|CHR$}}(128) + {{Cl|CHR$}}(72): {{Cl|KEY(n)|KEY}}(15) ON 'cursor up {{Cl|ON KEY(n)|ON KEY}}(16) {{Cl|GOSUB}} LPad {{Cl|KEY n|KEY}} 16, {{Cl|CHR$}}(128) + {{Cl|CHR$}}(75): {{Cl|KEY(n)|KEY}}(16) ON 'cursor left {{Cl|ON KEY(n)|ON KEY}}(17) {{Cl|GOSUB}} RPad {{Cl|KEY n|KEY}} 17, {{Cl|CHR$}}(128) + {{Cl|CHR$}}(77): {{Cl|KEY(n)|KEY}}(17) ON 'cursor right {{Cl|ON KEY(n)|ON KEY}}(18) {{Cl|GOSUB}} DnPad {{Cl|KEY n|KEY}} 18, {{Cl|CHR$}}(128) + {{Cl|CHR$}}(80): {{Cl|KEY(n)|KEY}}(18) ON 'cursor down {{Cl|DEF SEG}} = 0 DO numL = {{Cl|PEEK}}(1047) {{Cl|AND (boolean)|AND}} 32 '32 if on capL = {{Cl|PEEK}}(1047) {{Cl|AND (boolean)|AND}} 64 '64 on {{Cl|IF...THEN|IF}} numL {{Cl|OR (boolean)|OR}} capL {{Cl|THEN}} {{Cl|COLOR}} 12: {{Cl|LOCATE}} 13, 50: {{Cl|PRINT}} "Turn Num or Cap Lock OFF!" {{Cl|ELSE}} : {{Cl|COLOR}} 10: {{Cl|LOCATE}} 13, 50: {{Cl|PRINT}} "Number and Cap Lock OK! " {{Cl|SLEEP}} ' {{Cl|KEY n|KEY}} or {{Cl|TIMER}} event breaks a sleep {{Cl|END IF}} {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) {{Cl|DEF SEG}} {{Cl|KEY(n)|KEY}}(15) OFF: {{Cl|KEY(n)|KEY}}(16) OFF: {{Cl|KEY(n)|KEY}}(17) OFF: {{Cl|KEY(n)|KEY}}(18) OFF {{Cl|KEY(n)|KEY}}(11) OFF: {{Cl|KEY(n)|KEY}}(12) OFF: {{Cl|KEY(n)|KEY}}(13) OFF: {{Cl|KEY(n)|KEY}}(14) OFF {{Cl|END}} UpPad: COLOR 14: LOCATE 11, 26: PRINT " Up cursor pad " {{Cl|RETURN}} LPad: COLOR 14: LOCATE 11, 26: PRINT "Left cursor pad " {{Cl|RETURN}} RPad: COLOR 14: LOCATE 11, 26: PRINT "Right cursor pad" {{Cl|RETURN}} DnPad: COLOR 14: LOCATE 11, 26: PRINT "Down cursor pad " {{Cl|RETURN}} UpNum: COLOR 11: LOCATE 11, 26: PRINT " Up number pad " {{Cl|RETURN}} LNum: COLOR 11: LOCATE 11, 26: PRINT "Left number pad " {{Cl|RETURN}} RNum: COLOR 11: LOCATE 11, 26: PRINT "Right number pad" {{Cl|RETURN}} DnNum: COLOR 11: LOCATE 11, 26: PRINT "Down number pad " {{Cl|RETURN}} {{CodeEnd}} : ''Explanation:'' The Number Lock or Caps Lock keys ON may hinder extended key reads in QBasic but not QB64!([[#toc|Return to Table of Contents]])
{{PageSeeAlso}} * [[KEY LIST]], [[ON KEY(n)]] * [[KEY(n)]], [[INKEY$]] * [[_KEYHIT]], [[_KEYDOWN]] * [[Keyboard scancodes]] {{PageNavigation}}