Teen Programmers Unite  


Return to forum top

keyboard buffer

Posted by DanielPaval [send private reply] at June 06, 2003, 04:13:38 PM

Does anyone know a tutorial about using the keyboard buffer or something like this?

If not, do you know a function to empty the keyboard buffer?

Posted by CodeX [send private reply] at June 06, 2003, 04:33:06 PM

keyboard buffer data is in the BIOS data area i believe.
*runs to get book on assembler*
Okay, here's the dat for the keyboard buffer.
At address 0040:0017 (hex of course), your have your keyboard status flags in this arrangement:

bit 7: insert is on
bit 6: caps lock is on
bit 5: num lock is on
bit 4: scroll lock is on
bit 3: either Alt key pressed
bit 2: either control key pressed
bit 1: left shift key pressed
bit 0: right shift key pressed

At the next byte, 0040:0018, you have your second set of flags.

bit 7: insert key pressed
bit 6: caps lock pressed
bit 5: num lock pressed
bit 4: scroll lock pressed
bit 3: pause state is on
bit 2: sys req key is presed
bit 1: left alt key pressed
bit 0: left control key pressed

your next byte, 0040:0019 contains the alt+number workspace used for special characters and things like that. At 0040:001A, you have a word that points to the beginning of the keyboard buffer, and the next word points to the end of the keyboard buffer. The next 32 bytes are the area used as the keyboard buffer by default.

The next set of keyboard status bytes are located at 0040:0096. The first byte contains this data:
bit 7: read-ID in progress
bit 6: last code read was first of two ID codes (for extended keyboard support)
bit 5: force num lock if read-ID and enhanced keyboard
bit 4: enhanced keyboard installed
bit 3: right alt key pressed
bit 2: right control key pressed
bit 1: code E0h was read from keyboard (for certain characters)
bit 0: code E1h was read from keyboard (same thing here)

the next byte, 0040:0097, contains this info:
bit 7: keyboard transmit error flag
bit 6: led update in progress
bit 5: RESEND recieved from keyboard
bit 4: ACK recieved from keyboard
bit 3: reserved, must be zero
bit 2: caps lock LED
bit 1: num lock LED
bit 0: scroll lock LED

That's about all the information you can get from the keyboard (via BIOS at least). I suppose the easiest way is to just allocate a certain amount of memory to your program, clear it out, and reset the head and tail pointers to that area. I don't know if you can edit information in the BIOS data area so that might not be feasible. I'll try it just to see what happens and get back to you after doing some testing.

Posted by CodeX [send private reply] at June 06, 2003, 05:56:59 PM

Can't edit for some reason. After doing some tests, i have discovered that either this book is seriously out-of-date, my assembler isn't working properly, or i can't code very well in assembler.

Posted by DanielPaval [send private reply] at June 07, 2003, 12:56:52 AM

I only need to erase the buffer. When pressing a key for a long time it is like pressing it more times. In my programe when a certain key is pressed an animation begins. It runs for some time (150ms). Until it ends the buffer loads the key more times if I keep pressing it and the animation runs for each of them. The animation is in fact a little man moving on the screen. The problem is that when I stop pressing the direction keys it still moves because there are keys left to be analized in the keyboard buffer. So I have to erase the keyboard buffer after each animation is finished. Just that. There isn't probably a predefined function for this. If you know any way to do this please reply because I don't have much time to finish my project.


Posted by CViper [send private reply] at June 07, 2003, 04:30:25 AM

What platform are you coding on?

Anyway, instead of using a keyboard buffer, simply poll keystates each frame:

if( key_down( whatever_key ) && !animation_running() )
Posted by sphinX [send private reply] at June 07, 2003, 06:01:36 PM

You haven't really specified on what platform you're running. If you're programming in DOS, the recommended way to create keyboard handling in a game is to in fact completely replace the keyboard handling interrupt (int 8 i believe) routine with something of your own --- see something like the Allegro game programming library for an example or do a Google search.

Posted by DanielPaval [send private reply] at June 08, 2003, 12:58:49 PM

I'm programming in C.

Posted by Psion [send private reply] at June 08, 2003, 01:33:52 PM

That's not a platform. You should include operating system and processor.

Posted by DanielPaval [send private reply] at June 09, 2003, 12:36:14 PM

Windows, Athlon Xp2000+

Posted by CViper [send private reply] at June 09, 2003, 01:54:05 PM

It's still hard to tell what exactly is going on, but my guess is that you play the animation inside the message handler each time you recive a WM_KEYDOWN or WM_CHAR - effectivly blocking the message handler for 150 ms. That way all messages are queued until the animation stops and the message handler returns.

The solution is to either start a thread which will do the animation or create a timer which will fire every x ms and send you a WM_TIMER message. Each time you recive a WM_TIMER message, you update the animation by one frame.

I'd suggest you look into multithreading. (check CreateThread() on the MSDN)

Posted by DanielPaval [send private reply] at June 10, 2003, 10:02:23 AM

Thanks for all your advices and ideas. They're more complicated than I thought. I just wanted a piece of code that erases the keyboard buffer if this is possible.
Anyway I managed to get over this problem by disabling the animation.

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.