www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/23/10:46:58

Xref: news2.mv.net alt.msdos.programmer:13722 comp.os.msdos.djgpp:6311 comp.os.msdos.programmer:26868
From: boylesgj AT lion DOT cs DOT latrobe DOT edu DOT au (Gregary J Boyles)
Newsgroups: alt.msdos.programmer,comp.os.msdos.programmer,comp.os.msdos.djgpp
Subject: An key board handler ISR question.
Date: 23 Jul 1996 13:42:17 GMT
Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
Lines: 131
Distribution: world
Message-ID: <4t2krp$8jp@lion.cs.latrobe.edu.au>
NNTP-Posting-Host: lion.cs.latrobe.edu.au
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Could any one tell me if there is any reason why this key board handler may
fail. It is based on some info from Game Programmer's Encyclopedia but there
was not enough info for me to be absolutely sure that this will work.

You will notice refferences to a character table which is read in from a file
by the function which installs the ISR (not included). Basically it looks up
the scan code in a 2-D array where the scan code is the first array index and
the second index is determined by the state of the numlock, capslock and 
shift keys. The table has also not been included due to its size.

I am writing this with Borland C++ 3.1.

#include <dos.h>


/*
*****************************************************************************
*                                                                           *
*                           THE KEY BOARD HANDLER                           *
*                                                                           *
*****************************************************************************
*/


// Shift key flags.
bool NumLockOn=false,CapsLockOn=false,LeftShiftOn=false,RightShiftOn,
     CtrlOn=false,AltOn=false;


// The character table.
const int Width=88,Height=8;
unsigned Table[Width][Height];


// The key buffer.
const int BuffSize=50;
unsigned Buff[BuffSize];
int BuffPos=0;


// The key board ISR.
void interrupt KeyBoardISR(...)
{
  const unsigned char ExtendedCode=224,ScrollLockScanCode=70,CapsLockScanCode=58,
                      NumLockScanCode=69,LeftShiftScanCode=42,RightShiftScanCode=54,
                      AltScanCode=56,CtrlScanCode=29,Bit7Mask=128,AckValue=0x20;
  const unsigned KeyBoardPort=0x60,AckPort=0x20;



  unsigned char ScanCode=0;

  // Read the key's scan code from the key board port.
  ScanCode=inportb(KeyBoardPort);

  // Acknowledge the interrupt.
  outportb(AckPort,AckValue);

  // If the code 224 was read then an extended key was pressed and its scan
  // code will be available next time around so do nothing.
  if (ScanCode==ExtendedCode)
  {
  }
  // If there is room in the key buffer to put another key press.
  else if (BuffPos<BuffSize)
  {
    // If CapsLock pressed then toggle CapsLock flag.
    if (ScanCode==CapsLockScanCode)
      CapsLockOn=!CapsLockOn;

    // If NumLock pressed then toggle NumLock flag.
    else if (ScanCode==NumLockScanCode)
      NumLockOn=!NumLockOn;

    // If the left shift key was released then set LeftShift flag to false.
    else if (ScanCode==(LeftShiftScanCode | Bit7Mask))
      LeftShiftOn=false;

    // If the left shift key was pressed then set LeftShift flag to true.
    else if (ScanCode==LeftShiftScanCode)
      LeftShiftOn=true;

    // If the right shift key was released then set RightShift flag to false.
    else if (ScanCode==(RightShiftScanCode | Bit7Mask))
      RightShiftOn=false;

    // If the right shift key was pressed then set RightShift flag to true.
    else if (ScanCode==RightShiftScanCode)
      RightShiftOn=true;

    if (ScanCode==133)
      ScanCode=87;
    eif (ScanCode==134)
      ScanCode=88;
    // Scan codes range from 1 to 88 but the horizontal array bounds ranges
    // from 0 to 87 so subtract one from ScanCode.
    ScanCode--;

    // Determine the row to look up depending on the CapsLock, shift and NumLock
    // flags.
    int Row;
    bool ShiftOn=LeftShiftOn || RightShiftOn;
    if (CapsLockOn && !ShiftOn && NumLockOn)
      Row=0;
    else if (CapsLockOn && !ShiftOn && !NumLockOn)
      Row=1;
    else if (CapsLockOn && ShiftOn && NumLockOn)
      Row=2;
    else if (CapsLockOn && ShiftOn && !NumLockOn)
      Row=3;
    else if (!CapsLockOn && !ShiftOn && NumLockOn)
      Row=4;
    else if (!CapsLockOn && !ShiftOn && !NumLockOn)
      Row=5;
    else if (!CapsLockOn && ShiftOn && NumLockOn)
      Row=6;
    else if (!CapsLockOn && ShiftOn && !NumLockOn)
      Row=7;

    // Look the scan code up in the table and get the ASCII character or key
    // code.
    unsigned Key=Table[ScanCode][Row];
    Buff[BuffPos++]=Key;
  }
  else
  {
    sound(1000);
    delay(50);
    nosound();
  }
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019