Controlling The Keyboard Buffer



July 01, 1990
URL:http://www.drdobbs.com/controlling-the-keyboard-buffer/184402195

July 1990/Controlling The Keyboard Buffer

Steven Gruel is a programmer/analyst with Restaurant Integrated Computer Controls (RICC) in Norcross, GA. Presently he is working on operations control and communications on an organization-wide scale. His computer interests include C programming and assembler optimizing. You may contact him at 5175 Wexford Lane, Norcross, GA 30071.

Have you ever needed to ungetch() more than one character at a time, see how many characters are in the buffer, or even to know what the characters are without removing them from the buffer? Fortunately, Turbo C makes this easy and painless, without using interrupts or assembler. By controlling the circular character buffer and the two pointers that regulate the incoming and outgoing character data, you can create any situation in the buffer you wish.

How The Buffer Works

The buffer consists of 16 consecutive words of memory and two pointers. The first pointer is called the "Head", resides at 0x40:0x1A, and points to the next word in the buffer to be read by getch() or other similar functions. The second pointer is called the "Tail" and is positioned at 0x40:0x1C. It points to the location where the next keyboard interrupt will leave its data. When the buffer is empty, Head and Tail both point to the same place, making them equal. As keys are added to the buffer, the Tail is incremented by twos. Once Tail exceeds 60, it will restart at 30, giving the buffer its circular behavior. The Tail will continue moving in this circle until it has filled the word behind the Head. At this point the Tail will not be increased, and all subsequent keys sent by the keyboard interrupt will merely beep and be ignored until the Head is moved by a get-character function. When keyboard data is brought into the C program, the Head is decremented one word per key. When getch() is called the first time, it returns 0, moves the scan code to character byte, and does not change the Head until Head reaches the location one word short of the Tail, indicating that it's full.

How To Use The Functions

poll_kb() checks the buffer for any characters that may be available and returns either the character or zero, the latter indicating no data available.

readch() uses poll_kb() in a loop to wait for a non-zero result. readch() returns the non-zero result much like getch() except that it shifts left any extended character's scan code in place of the ASCII code.

push_kb() puts a scan code and ASCII character code into the buffer and moves the Tail accordingly, just like the key_board interrupt does.

kb_count() simply returns the number of keystrokes stored in the buffer.

kb_peek() looks ahead the number of reads specified, without changing any data or positions.

kb_clear() sets Head and Tail to the same value, creating an empty buffer.

Note that poll_kb(), readch() and kb_peek() return all extended characters on their first call as the scan code (i.e., the extended part of the character code) logically shifted left eight bits. Any return value of 0 means there is no data in the buffer for this call.

Applications

You can use these functions in a variety of situations, such as teaching new users to use a software package. For example, you could store keyboard sequences in a file along with prompts for popup messages. Your interactive tutorial program must first disable the keyboard interrupt by storing the current interrupt address and replacing it with an empty function. Then you must tag onto one of the system clock interrupts to read the file data, check the keyboard buffer, insert key codes, and to call the message and time delay handler. These tasks could either be linked into the target program or coded as a TSR.

You should also consider using the keyboard buffer as an easy way to add macros to your existing code with a minimum of fuss. Another interesting application might be to turn control over to a remote location through a modem or network or to provide technical support with a more hands-on approach.

Listing 1

July 1990/Controlling The Keyboard Buffer/Listing 1

Listing 1

#include <dos.h>
#include <conio.h>

void show_char();
int  poll_kb();
int  readch();
int  push_kb();
int  kb_count();
int  kb_peek();
void kb_clear();

void main(void)        /*  testing the KB functions */
   {
   int x, y;
      
      /*  Put some characters into the buffer */
      /*  For most applications the scan codes are not */
      /*  important, but for extended char it must have */
      /*  the scan code and 0 for the char: */
   for (x : 0; x < 12; x++)
      push_kb(1,32 + x);
   push_kb(59,0);
   
   printf("\n%d key(s) in the buffer. \n\n",kb_count());
   printf("Key number %d is '%c' \n\n", 5, kb peek(5));
   printf("Key number %d is '%c' \n\n", 7, kb_peek(7));
   
   show_char();
   }

void show_char(void)     /*  This is just a test function */
   {
   int c;
   while((c = readch()) != 59 << 8)         /*  27 = ESC */
      {
      putch(c);
      printf(" the code for this is %d \n",c);
      }
   putch(c);
   printf(" the code for this is %d \n",c);
   }

/*  poll_kb(): returns 0 if the buffer is empty, */
/*  else ascii code or scan codes * 256 for extended: */

int poll_kb(void)
   {
   int head, tail;
   unsigned int code;
   
   head = peek(0x40, 0x1A);
   tail = peek(0x40, 0x1C);
   
   if(head == tail)
      return(0);
   
   code = (peek(0x40,head) & 0xFF);
   if(code == 0)                       /*  extended char */
      code = (peek(0x40,head) & 0xFF00);
   
   if(head < 60)
      poke(0x40,0x1A,(head += 2));
   else
      poke(0x40,0x1A,(head = 30));
   
   return(code);
   }

/*  readch(): keep checking buffer until something  */
/*       is there. Can also be done with "kbhit()"  */

int readch(void)
   {
   int code;
   while((code = poll_kb()) == 0)
      {       /*  can run any fast function here */
      }
   return(code);
   }

/*  putsh_kb(): key = the key code, ascii = asc code */
int push_kb(int key, int ascii)
   {
   unsigned int code;
   int head, tail;
   
   head = peek(0x40, 0x1A);
   tail = peek(0x40, 0x1C);
   
   code = (key << 8) + ascii;
   poke(0x40,tail,code);
   
   if(tail < 60)
      {
      tail += 2;
      if(tail == head)
         return(0);
      poke(0x40,0x1C,tail);
      }
   else
      {
      tail = 30;
      if(tail == head)
         return(0);
      poke(0x40,0x1C,tail);
      }
   return(1);
   }

int kb_count(void)
   {
   int head, tail;
   int count;
   
   head = peek(0x40, 0x1A);
   tail = peek(0x40, 0x1C);
   
   if(head == tail)    /*  buffer is empty */
      return(0);
   
   if(head > tail)     /*  adjust for the roll over */
      tail += 32;
   count = (tail - head) / 2;
   
   return(count);
   }
/*  "kb_peek" will return the ascii code of  */
/*  the number of keys back specified.       */
/*  If it is an extended char the scan code  */
/*  will be left shifted 8 and returned      */

int kb_peek(int key_number)
   {
   int head, tail, code;

/*  see if key_number of items are in the buffer: */
   
   if(key_number > kb_count())
      return(0);
   
   head = peek(0x40, 0x1A);
   tail = peek(0x40, 0x1C);
   
   /*  advance the desired number of words: */
   head += (key_number - 1) * 2;
   if(head > 60)
      head -= 32;      /*  if head needs to wrap around */
   
   code = (peek(0x40,head) & 0xFF);
   
   if(code == 0)   /*  exten. scan sent instead of ascii */
      code = (peek(0x40,head) & 0xFF00);
   
   return(code);
   }

void kb_clear(void)  /*  empty buffer data */
   {
   int head, tail;
   
   head = peek(0x40, 0x1A);
   tail = peek(0x40, 0x1C);
   
   poke(0x40,0x1C,head);  /*  set tail = head */
   }

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.