INKEY$ function

Purpose

Read a keyboard character or mouse event from the console input buffer, without echoing it to the screen.  INKEY$ will return immediately, even if there are no character or mouse events to retrieve.

Syntax

i$ = INKEY$

Remarks

INKEY$ returns a string of 0, 1, or 2 characters that reflects the status of the keyboard buffer.  It can also return a string of four bytes if mouse event trapping is enabled.  INKEY$ will always read keys from the keyboard, regardless of STDIN redirection status.

A null string (LEN(i$) = 0) means that the buffer is empty (no key or mouse event is available).

A string length of one (LEN(i$) = 1) means that an ASCII key was pressed and the string contains the ASCII character.  Values between 1 and 31 indicate a control code.

A string length of two (LEN(i$) = 2) means that an extended key was pressed.  In this case, the first character in the string has a value of zero, and the second is the extended keyboard code.  For example, pressing the F1 key will return CHR$(0,59).

A string length of four (LEN(i$) = 4) means that a mouse event was retrieved.  In this case, the first two characters in the string have a value of 255.  The third character is an event code:

Value

Definition

1

movement

2

double-click

4

button press

8

button release

The fourth character defines the button that was pressed:

Value

Definition

0

no button

1

left

2

right

4

second left button

8

third left button

16

fourth left button

For example, moving the mouse with the left button pressed will return CHR$(255, 255, 1, 1).  Button presses can be combined, for example pressing the left and right mouse buttons together will return CHR$(255, 255, 4, 3).  The Mouse must be enabled with the MOUSE statement in order to receive mouse events.

If you are converting DOS code to PB/CC, you are likely to come across the following type of loop:

WHILE NOT INSTAT : WEND

a$ = INKEY$

or:

DO

  a$ = INKEY$

LOOP UNTIL LEN(a$)

While both of these loops work fine in PB/CC, they take as much CPU attention as possible.  As a result, this type of loop is very wasteful and can cause other applications to work sluggishly, even when the PB/CC app is running in the background.  The solution is to add a SLEEP 0 statement within the loop (to release the current CPU time-slice to other tasks) or, better, use the WAITKEY$ function instead.

See also

INPUT FLUSH, INSHIFT, INSTAT, LINE INPUT, MOUSE, MOUSESTAT, MOUSEX, MOUSEY, WAITKEY$, WAITSTAT