INKEY$ Returns Two-Byte String for Arrow and Function Keys (30844)



The information in this article applies to:

  • Microsoft Visual Basic for MS-DOS
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b

This article was previously published under Q30844

SUMMARY

The INKEY$ function returns a one- or two-byte string. Alphanumeric keys return one-byte strings. The function keys and the directional keys return two-byte strings.

If INKEY$ returns a two-byte string, then each byte needs to be examined to determine the key that was pressed. For two-byte strings, the first byte will always be null [an ASCII value of zero, CHR$(0)], and the second byte will be the key's scan code. The LEN function tells you the length of the returned string (one byte or two).

The ASCII and keyboard scan codes are listed in Appendix A of the Microsoft Visual Basic version 1.0 for MS-DOS Reference Manual.

MORE INFORMATION

To execute the following examples in VBDOS.EXE, use the steps listed below:

  1. From the File menu, choose New Project.
  2. Copy the code example to the Code window.
  3. Press F5 to run the program.

Example 1

The following sample code prints a message if the UP ARROW key is pressed:
top:
I$ = INKEY$
'The scan code for the UP ARROW is &H48:
IF I$ = CHR$(0) + CHR$(&H48) THEN

   PRINT "up arrow key pressed"

END IF
GOTO top
				

Example 2

The following code example shows how to print the ASCII value of any key pressed:
REM     ASCII.BAS
CLS
PRINT "Hit F1 To Exit"
PRINT:PRINT
DO

   A$=INKEY$
   IF MID$(A$,1,1) <> "" THEN

      PRINT A$
      PRINT "ASCII Value = "; ASC(A$)

   END IF
   IF MID$(A$,2,1) <> "" THEN
      PRINT "ASCII Value = "; ASC(MID$(A$,2,1))

   END IF

LOOP UNTIL MID$(A$,2,1) = CHR$(59)
END
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB30844