Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  The scan-code translation job is moderately complicated because the IBM
  keyboard recognizes two types of keys that change the meaning of a
  keystroke: shift keys and toggle keys.

  The shift keys

  Three keys--Ctrl, Shift, and Alt--are known as shift keys: They change the
  shift state, and thereby the meaning, of whatever key they are used with.
  For example, when you press Shift-C, you get a capital C; when you press
  Ctrl-C, you generate the "break" character. The ROM BIOS recognizes that
  all subsequent key actions will be influenced by that shift state as long
  as a shift key is pressed.

  The toggle keys

  In addition to the shift keys, two toggle keys also affect the keyboard's
  shift state: the Caps Lock key and the Num Lock key. When activated, Caps
  Lock reverses the shift state of the alphabet keys; it doesn't affect the
  other keys. When activated, the Num Lock key disables cursor-control
  functions on the numeric keypad. Toggle keys are activated with a single
  keystroke and remain active until released by a second keystroke.

  The shift-key and toggle-key status information is kept by the ROM BIOS in
  a low-memory location (0040:0017H), where you can use or change it. When
  you press a shift key or a toggle key, the ROM BIOS sets a specific bit in
  one of these two bytes. When the ROM BIOS receives the release scan code
  of a shift key, it switches the status bit back to its original shift
  state.

  Whenever the ROM BIOS receives a scan code for an ordinary keystroke, such
  as the letter z or a right arrow key, it first checks the shift state and
  then translates the key into the appropriate 2-byte code. (We'll discuss
  the status bytes in more detail on page 137.)

  The combination keys

  While the ROM BIOS routine is translating scan codes, it checks for Sys
  Req keystrokes and for certain shift-key combinations; specifically, it
  checks for the Ctrl-Alt-Del, Shift-PrtSc, Ctrl-Num Lock, and Ctrl-Break
  combinations. These five command-like key actions cause the ROM BIOS to
  perform a specific task immediately.

  Ctrl-Alt-Del causes the computer to reboot. Ctrl-Alt-Del is probably used
  more often than any other special key combination. It works dependably as
  long as the keyboard interrupt service is working. If the interrupt
  service is not working, turn the power off, wait a few seconds, then turn
  it on again; the power-on program resets all interrupt vectors and
  services.

  Shift-PrtSc (Print Screen on the 101/102-key keyboard) causes the ROM BIOS
  interrupt 09H handler to execute software interrupt 05H. The default
  interrupt 05H handler is also part of the ROM BIOS; it prints a "snapshot"
  of the current contents of the screen.

  Ctrl-Num Lock (Pause on the 101/102-key keyboard) suspends operation of a
  program until another keystroke occurs.

  Ctrl-Break causes the ROM BIOS to generate software interrupt 1BH and to
  set bit 7 of the byte at 0040:0071H to 1. The default DOS handler for
  interrupt 1BH simply sets a flag internal to DOS that causes DOS to
  interpret Ctrl-Break as Ctrl-C. You can override the default DOS action
  for Ctrl-Break by pointing the interrupt 1BH vector (located at
  0000:006CH) to your own interrupt handler.

  Sys Req (on the 84-key keyboard) and Alt-Sys Req (on the 101/102-key
  keyboard) cause the ROM BIOS to issue interrupt 15H with AH = 85H. Your
  program can provide its own interrupt 15H handler that intercepts and
  processes Sys Req keystrokes. (See Chapter 12 for details.)

  These are the only key combinations that are especially meaningful to the
  ROM BIOS. When an invalid combination is reported from the keyboard, the
  ROM BIOS simply ignores it and moves on to the next valid key action.

  Two more features of the PC keyboard should be presented before we discuss
  the details of keyboard coding: repeat key action and duplicate keys.

  Repeat key action

  The PC keyboard features automatic repeat key action, a process called
  typematic by IBM. The circuitry inside the keyboard monitors how long each
  key is pressed, and if a key is held down longer than a defined interval,
  the circuitry generates repeat key actions. This typematic action is
  reported as successive keystroke scan codes, without the intervening
  key-release codes. This makes it possible for an interrupt 09H handler to
  distinguish between actual keystrokes and typematic action. However, the
  ROM BIOS does not always distinguish between the two. The ROM BIOS
  keyboard-handling routine treats each automatic repeat key action as
  though the key were actually pressed and interprets the key accordingly.

  --------------------------------------------------------------------------
  Keyboard-enhancer programs
    Thanks to the flexible software design of the PC, it's possible to
    create programs that customize the keyboard. Such programs are called
    keyboard-enhancer programs.

    Keyboard-enhancer programs monitor the scan codes that come in from the
    keyboard and respond to them in ways that aren't supported by the ROM
    BIOS or by DOS. Typically, these programs are fed instructions, called
    keyboard macros, that tell them what keystrokes to look for and what
    changes to make. The change might involve suppressing a keystroke
    (acting as if it never happened), replacing one keystroke with another,
    or replacing one keystroke with a long series of keystrokes. The most
    common use of keyboard macros is to abbreviate frequently used phrases;
    for example, you might instruct a keyboard enhancer to convert a key
    combination, such as Alt-S, into a salutation you use in your
    correspondence, such as Sincerely yours. You can also use keyboard
    macros to condense multiple-keystroke program commands to a single
    keystroke.

    Keyboard enhancers work by combining the powers of two special
    facilities--one that's part of DOS and one that's part of the PC's ROM
    BIOS. The DOS facility allows the enhancer program to remain resident in
    the computer's memory, quietly monitoring the operation of the computer
    while the ordinary control of the computer is turned over to a
    conventional program, such as a word processor. The ROM BIOS facility
    lets programs divert the stream of keyboard information so that it can
    be inspected and changed before it is passed on to a program. These
    programs use the DOS Terminate and Stay Resident facility to stay active
    in memory while other programs are run; then they use the ROM BIOS
    keyboard-monitoring facility to preview keyboard data and change it as
    needed.
  --------------------------------------------------------------------------

  For example, if you press and hold the A key long enough for the keyboard
  to begin generating successive keystroke signals, then the ROM BIOS will
  create a series of As to be passed on to whatever program is reading
  keyboard data. On the other hand, when you press and hold a shift key, the
  ROM BIOS sets bits in its status bytes in segment 40H. While you hold the
  shift key down, the ROM BIOS continues to set the same bits to 1. When you
  release the key, the ROM BIOS resets the status bits. All this boils down
  to the simple fact that the ROM BIOS treats repeat key actions in a
  sensible way, acting on them or ignoring them as needed.

  Duplicate keys

  We've already described how the keyboard differentiates duplicate keys by
  assigning different scan codes to each. The ROM BIOS translates duplicate
  keys into the same ASCII character codes. For example, if you press either
  of the two asterisk keys, the ROM BIOS returns ASCII 2AH (the ASCII code
  for an asterisk); if you press either of the two Ctrl keys on a
  101/102-key keyboard, the ROM BIOS sets the appropriate bit in its
  shift-state byte.

  The ROM BIOS also lets programs tell the difference between duplicate
  keys, in some cases. Remember that the ROM BIOS translates each keystroke
  into a scan code as well as an ASCII code. A program that requests a
  keystroke from the ROM BIOS can inspect the scan code to determine which
  key was pressed. In the case of shift keys, a program can inspect the BIOS
  shift-state bytes at 0040:0017H and 0040:0018H to determine exactly which
  shift keys are pressed. (See the discussion of the shift-state bytes on
  pages 137 and 138.)

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson