Introduction
This document which tells you what to do when the inkey$ function
isn't enough. Two of the annoying inkey$ "features" are:
- Only one key may be pressed.
- The rate of keyboard codes are set by the OS.
Reading from the keyboard
The keyboard is connected to port 60h. To read a keycode from the keyboard directly, use the command:
key = INP(96)
You may wonder where '96' came from. The hexadecimal value 60 is equal to the decimal value 96.
The value you get is a scancode. It is not the ASCII value for the key. The ESC key have the scancode 1, for example.
One scancode is used when the key is pressed, and another when the key is released. That is how you now if a key is pressed or not.. it is pressed until the release code comes.
Please, don't beep
The OS will read from the keyboard too, and when you have pressed a few keys the computer will beep. This is because the keyboard buffer is full. To remove a value from the keyboard buffer, I usually use the inkey$ function:
junk = inkey$
The only purpose of the command above was to remove a value from the keyboard buffer. The value in junk can be ignored.
Example code
This example program will read scancodes from the keyboard, and print them to the screen.
PRINT "keyboard program"
PRINT "press ESC to quit"
DIM scancode
DIM oldscancode
DIM junk$
' Loop while ESC is not pressed
WHILE scancode <> 1
' Previous scancode
oldkeycode = scancode
' Empty the keyboard buffer
junk$ = INKEY$
' Read scancode from keyboard
scancode = INP(96)
' Only print each code once
IF scancode <> oldscancode THEN
PRINT scancode
END IF
WEND