INKEY$
This page was last modified 19:36, 23 February 2019 by Mars2000you. Based on work by Gdx and Snout.

Contents

Effect

Returns either a single character read from the keyboard or (if no key is pressed) an empty string.

This instruction can be combined with CHR$() to check if a specific key has been pressed. It's useful not only for the alphanumeric keys, but also the cursor keys, the spacebar or another special key. However, if you want to make your game or application compatible with joysticks, it's better to directly use the STICK() and STRIG() instructions.

Special keys common to all MSX machines

Code for CHRS() Key Code for CHRS() Key Alternative *
8 BS 27 ESC
9 TAB 28 STICK(n) = 3
11 HOME 29 STICK(n) = 7
12 CLS 30 STICK(n) = 1
13 RETURN 31 STICK(n) = 5
18 INS 32 SPACE STRIG(n) = -1
24 SELECT 127 DEL
  • n is a value between 0 and 2 for STICK(), 0 and 4 for STRIG()

Examples

10 A$=INKEY$
20 IF A$="" THEN 10
30 PRINT "You pressed ";A$;" - CHR$(";ASC(A$);")"
 
RUN
You pressed M - CHR$( 77 )

Note: The result will be an 'empty' character with some special keys.

10 A$=INKEY$
20 IF A$=CHR$(28) THEN 30 ELSE 10
30 PRINT "You pressed the key to move the cursor to the right"

Related to

CHR$(), INPUT$, STICK(), STRIG()

Compatibility

MSX-BASIC 1.0 or higher