{***********************************************************************}
{									}
{ __QueueKey  Return the available space in the keyboard buffer 	}
{									}
{ function  __QueueKey : word;						}
{									}
{ Description This function reports the available space in the BIOS	}
{	      type-ahead buffer.  If the returned value is to be used,	}
{	      you should turn off interrupts (Inline($FA)) to prevent	}
{	      further keystrokes or other processes from changing the	}
{	      number.  Enable (Inline($FB)) interrupts once you have	}
{	      finished manipulating the buffer				}
{									}
{	      __QueueKey assumes that the BIOS type-ahead buffer is at	}
{	      the standard location of $0040:$001A. Like __StuffKey,	}
{	      if a resident program has "moved" the keyboard buffer,	}
{	      it will not function as expected. 			}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

function __QueueKey : word;

  const
    BUFFERSIZE = 32;		       { Keyboard buffer size		}

  var
    KbdBuffer  : _KeyBoard absolute _BIOSSEG:$001A;
    Avail      : word;

  begin {__QueueKey}

    with KbdBuffer do
      if (_BufferHead > _BufferTail) then
	Avail := (_Bufferhead - _Buffertail)
      else
	Avail := (_Bufferhead + BUFFERSIZE - _Buffertail);

    { The actual space for keystrokes is Avail divided by 2 (one	}
    { byte is needed for the character, and one is needed for the	}
    { scan code) minus 1 since one location is needed for overhead.	}

    __QueueKey := (Avail shr 1) - 1

  end;	{__QueueKey}
