Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Controlling The Keyboard Buffer

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.


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

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.