The [[INKEY$]] function returns user input as [[ASCII]] [[STRING]] character(s) from the keyboard buffer.
{{PageSyntax}}
: {{Parameter|keypress$}} = [[INKEY$]]
{{PageDescription}}
* Returns [[ASCII]] character string values in upper or lower cases. See: [[UCASE$]] and [[LCASE$]]
* Returns "" if no key has been pressed since the last keyboard read.
* Some control keys cannot be read by INKEY$ or will return 2 byte [[ASCII]] codes.
* INKEY$ can also be used to clear a [[SLEEP]] key press or the keyboard buffer in a loop.
* Assign the INKEY$ return to a string variable to save the key entry.
* {{InlineCode}}{{Cl|LOCATE}} , , 1{{InlineCodeEnd}} displays the INKEY$ cursor. Use {{InlineCode}}{{Cl|LOCATE}} , , 0{{InlineCodeEnd}} to turn it off.
* To receive input from a [[$CONSOLE]] window, use [[_CINP]].
* Returns can be evaluated as certain [[ASCII]] characters or codes.
CHR$(0) + CHR$(76) [5 NumberPad] "L" (NumLock off in QB64)
CHR$(0) + CHR$(77) [→] Arrow "M"
CHR$(0) + CHR$(79) [End] "O"
CHR$(0) + CHR$(80) [↓] Arrow "P"
CHR$(0) + CHR$(81) [Page Down] "Q"
CHR$(0) + CHR$(82) [Insert] "R"
CHR$(0) + CHR$(83) [Delete] "S"
CHR$(0) + CHR$(84-93) [Shift] + F1-10
CHR$(0) + CHR$(94-103) [Ctrl] + F1-10
CHR$(0) + CHR$(104-113) [Alt] + F1-10
CHR$(0) + CHR$(114-119) [Ctrl] + keypad
CHR$(0) + CHR$(120-129) [Alt] + number
CHR$(0) + CHR$(130 or 131) [Alt] + _/- or +/= "é" or "â"
CHR$(0) + CHR$(133) [F11] "à"
CHR$(0) + CHR$(134) [F12] "å"
CHR$(0) + CHR$(135) [Shift] + [F11] "ç"
CHR$(0) + CHR$(136) [Shift] + [F12] "ê"
CHR$(0) + CHR$(137) [Ctrl] + [F11] "ë"
CHR$(0) + CHR$(138) [Ctrl] + [F12] "è"
CHR$(0) + CHR$(139) [Alt] + [F11] "ï"
CHR$(0) + CHR$(140) [Alt] + [F12] "î"
{{WhiteEnd}}
:In '''QB64''', [[CVI]] can be used to get the [[_KEYDOWN]] 2-byte code value. Example: '''{{text|status <nowiki>=</nowiki> _KEYDOWN(CVI(CHR$(0) + "P"))|green}}'''
{{PageExamples}}
''Example 1:'' Clearing the keyboard buffer after [[SLEEP]] delays for later [[INPUT]].
{{CodeStart}}{{Cl|PRINT}} "Press any keyboard typing key to end SLEEP"
L = {{Cl|LEN}}(entry$) ' check entry length for possible backspace
{{Cl|IF}} K$ = {{Cl|CHR$}}(46) {{Cl|AND (boolean)|AND}} flag = 0 {{Cl|THEN}} entry$ = entry$ + K$: flag = 1: mark = L ' decimal point
{{Cl|IF}} K$ = {{Cl|CHR$}}(8) {{Cl|AND (boolean)|AND}} L > 0 {{Cl|THEN}} ' backspace pressed and entry has a length
entry$ = {{Cl|MID$}}(entry$, 1, L - 1) ' remove one character from entry$
{{Cl|IF}} {{Cl|LEN}}(entry$) < mark {{Cl|THEN}} flag = 0 ' allow decimal point entry if other was removed.
{{Cl|LOCATE}} {{Cl|CSRLIN}}, {{Cl|POS}}(0) - 1: {{Cl|PRINT}} {{Cl|SPACE$}}(1); ' remove end character from screen
{{Cl|END IF}}
{{Cl|LOCATE}} 10, 32: {{Cl|PRINT}} entry$; ' display entry to user (semicolon required for correct {{Cl|POS}})
{{Cl|LOOP}} {{Cl|UNTIL}} K$ = {{Cl|CHR$}}(13) {{Cl|AND (boolean)|AND}} L > 0 'assures something is entered
{{CodeEnd}}
: ''Explanation:'' [[SLEEP]] waits for a keypress. It also allows background programs to share the processor and it leaves the keypress in the buffer for INKEY$. Keyboard string number characters range from [[ASCII]] codes 48 to 57. Any other entry is ignored by the IF statement. A decimal point (code 46) entry is allowed once in the input. The flag value stops further decimal point additions. Backspacing (code 8) is also allowed if the entry has at least one character. The cursor column returned by [[POS]](0) reverts too after the end of the entry when printed each loop. The loop exits when [Enter] (code 13) is pressed and the entry has a length.
''Example 5:'' Using arrow keys to move a text character. A change from a previous position tells program when to PRINT:
{{CodeStart}}movey = 1: movex = 1 'text coordinates can never be 0
at$ = "@" 'text sprite could be almost any ASCII character
{{Cl|IF...THEN|IF}} b% > 119 {{Cl|AND (boolean)|AND}} b% < 130 {{Cl|THEN}} ' Alt + number codes only
C% = b% - 119 ' convert to actual number
{{Cl|IF...THEN|IF}} C% > 9 {{Cl|THEN}} C% = 0
num$ = num$ + {{Cl|LTRIM$}}({{Cl|STR$}}(C%))
{{Cl|END IF}}
{{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|LEN}}(num$) = 3 ' 3 digit codes only
{{Cl|PRINT}} num$
{{Cl|PRINT}} {{Cl|CHR$}}({{Cl|VAL}}(num$)
{{CodeEnd}}{{small|Code by Ted Weissgerber}}
{{OutputStart}} 155 ¢ {{OutputEnd}}
:''Explanation:'' Hold down Alt key and press 3 keyboard code number keys. '''Number pad keys may not work.''' Note that [[INKEY$]] cannot read Alt, Ctrl or Shift key presses without a key combination and the return is CHR$(0) + CHR$(code).
''See also:''
* [[_KEYHIT]], [[_KEYDOWN]], [[_MAPUNICODE]]
* [[_KEYCLEAR]]
* [[INPUT]], [[LINE INPUT]]
* [[INPUT$]], [[INP]]
* [[CHR$]], [[ASCII]]
* [[ASC]], [[Scancodes]] (keyboard)
* [[Windows_Libraries#Hot_Keys_.28maximize.29|Windows hot keys]]