BBS: Inland Empire Archive
Date: 02-01-93 (20:31)             Number: 353
From: HUGH MARTIN                  Refer#: NONE
  To: FRED PREUETT                  Recvd: NO  
Subj: INKEY$                         Conf: (2) Quik_Bas
Here's an old routine that once lived in the Basic Building Blocks
library before it was recoded in assembler to become Desqview and
Windows aware.  It was once a workhorse for me and is quite efficient.
You can use it whenever you want to check what's going on at the
keyboard.  It recognizes all the key combinations that the BIOS

'Program:  CheckKB.BAS             BBB library - Martin Systems 1991
'Purpose:  Checks the keyboard for a key press, and optionally waits
'          for one.  Returns the ASCII code for the key pressed, or
'          0 if no key pressed.
' Syntax:  call CheckKB (WaitFlag, KeyPressed)
'   Pass:  WaitFlag     wait for a key stroke flag
'                       (non-zero = wait, 0 = quick check only)
'Returns:  KeyPressed   an ASCII code for the key pressed, 0 if none
'   Note:  If WaitFlag is non-zero, the routine will wait until a key
'          is pressed and will return its ASCII code as KeyPressed.
'          If WaitFlag is zero, the routine quickly checks the keyboard
'          buffer without waiting and returns a key code via KeyPressed
'          if one was there.  If no key code was in the buffer,
'          KeyPressed is returned as zero.
'          Extended key codes are returned as 256 + ASCII code.
'Example:  do
'            call CheckKB (-1, KeyPressed)
'            print "Code for this key is:  "; KeyPressed
'          loop until KeyPressed = 27

defint a-z
sub CheckKB (WaitFlag, KeyPressed) static

  '-- get a character from the keyboard --
  if WaitFlag then                   'wait until a key is pressed
      r$ = inkey$                    'read keyboard
    loop until len(r$)               'repeat until a chr is found
  else                               'just check KB once for a chr
    r$ = inkey$                      'read keyboard
  end if

  '-- return an integer code --
  select case len(r$)                'check str len returned by inkey$
  case 0                             'null string - no chr waiting
    KeyPressed = 0                   'return a zero for the key code
  case 1                             'single chr - regular ASCII key
    KeyPressed = asc(r$)             'return its ASCII code
  case else                          '2 chrs - extended key pressed
    KeyPressed = asc(mid$(r$,2))+256 'get ASCII code of 2nd chr & add
  end select                         '  256 to prevent duplicate codes

end sub

--- Maximus 2.01wb
 * Origin: COSUG BBS: Colorado Springs PC User Group 719-632-2566 (1:128/13)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET