Board index » delphi » I need help with keyboard buffers!

I need help with keyboard buffers!

Hi,

This is my first posting to this ng... so here goes! :)

I have two questions for you programming people..

Firstly, how does one go about setting up and using multiple keyboard
buffers (for use in multiplayer games etc)? Preferably without machine code
:)

Secondly, is it possible to write a piece of code to write characters
DIRECTLY into the keyboard buffer? If so, how would I go about it? Do I
need to use special keyboard codes or can I get away with just writing
characters?

I am assuming that the keyboard buffer is just an area of main memory that
I can write to using the MOVE command... is this right?

Thanks for you help :)
--
--

--- Marcus : mar...@dushka.demon.co.uk -------------->
--- Web : http://www.dushka.demon.co.uk ------------->
----------------------------------------------------->

 

Re:I need help with keyboard buffers!


In article <01bc80c5$4b68c000$0100007f@dushka>, Marcus
<mar...@dushka.demon.co.uk> writes

Quote
>Secondly, is it possible to write a piece of code to write characters
>DIRECTLY into the keyboard buffer? If so, how would I go about it? Do I
>need to use special keyboard codes or can I get away with just writing
>characters?

>I am assuming that the keyboard buffer is just an area of main memory that
>I can write to using the MOVE command... is this right?

Yes you can use move and fiddle with the buffer but there are some TSR
programs that provide a bigger buffer by moving it.

On enhanced keyboards you can use INt 16h function 5h  (the
WriteKeyboard Function) :

(*** support ***)

var
   KeyStatusBits : byte absolute $0040:$0017;

var EnhancedKeyboard : boolean;

const   keyb_head : ^integer = ptr($40,$1A);
const   keyb_tail : ^Integer = ptr($40,$1C);
const   keyb_buff : ^char    = ptr($40,$0);
const   keyb_start: ^integer = ptr($40,$80);
const   keyb_end  : ^integer = ptr($40,$82);

FUNCTION IsEnhanced : Boolean;
VAR StateFrom16 : Byte;
var regs : registers;
BEGIN
  IsEnhanced := FALSE;
  asm
      mov ah,12h
      int $16
      mov StateFrom16,al
  end;

  IF StateFrom16 <> ShiftState THEN Exit;
  ShiftState := ShiftState XOR $20;

  asm
      mov ah,12h
      int $16
      mov StateFrom16,al
  end;

  IsEnhanced := StateFrom16 = ShiftState;
  ShiftState := ShiftState XOR $20;
END;

Function  KeyBufferFull : boolean;
var fpkeyhead, fpkeytail : pointer;
begin
      fpkeyhead := keyb_head;
      fpkeytail := keyb_tail;
      keybufferfull :=  ((integer(fpkeytail^)+2) = integer(fpkeyhead^))
or
                        ((Integer(fpkeytail^) = keyb_end^) and
                         (integer(fpkeyhead^) = keyb_start^));
end;

(*** main routine for this Q ***)

Procedure WriteKeyboard(st:string);
var charpos : byte;
var r:registers;
begin
     if not EnhancedKeyboard then exit;
     for charpos := 1 to length(st) do
     begin
          if KeyBufferFull then exit;
          c := ord(st[charpos]);
          asm
             mov ah,05h         {incidentally, putting the character in}
             mov ch,0           {ch instead of cl will write extended  }
             mov cl,c           {keyboard codes to the buffer.         }
             cli
             int 16h
             sti
          end;
     end;
end;

--
Pedt Scragg <postmas...@pedt.demon.co.uk>

In principle,   is there uncertainty that
Heisenberg was working his best in chaos?
=========================================

Re:I need help with keyboard buffers!


Quote
Marcus wrote:
> Hi,

> This is my first posting to this ng... so here goes! :)

> I have two questions for you programming people..

> Firstly, how does one go about setting up and using multiple keyboard
> buffers (for use in multiplayer games etc)? Preferably without machine code
> :)

Multiple keypresses aren't done by buffers.  It's done by reading raw
scan codes from the hardware.  I've written a unit which does this:

http://pw1.netcom.com/~setech/download/mkeys12.zip

Quote
> Secondly, is it possible to write a piece of code to write characters
> DIRECTLY into the keyboard buffer? If so, how would I go about it? Do I
> need to use special keyboard codes or can I get away with just writing
> characters?

BIOS calls are provided for this, and should be used.

Quote
> I am assuming that the keyboard buffer is just an area of main memory that
> I can write to using the MOVE command... is this right?

It's an area of memory, yes, but it's a wrap-around buffer that is
determined by several pointers.  Use int 16h, AH=05h (I believe).
Quote
> Thanks for you help :)
> --
> --

> --- Marcus : mar...@dushka.demon.co.uk -------------->
> --- Web : http://www.dushka.demon.co.uk ------------->
> ----------------------------------------------------->

Re:I need help with keyboard buffers!


I would like to underline the advisability of using BIOS calls rather
than writing directly to memeory. The 'easy' solution is tempting, but
in the long run, the discipline of using the 'right' approach pays
off.

Tom

http://www.arunet.co.uk/tkboyd/offers.htm
Freeware, shareware for IBM type pcs. Ideas for parents, teachers

Re:I need help with keyboard buffers!


Quote
TK Boyd wrote:

> I would like to underline the advisability of using BIOS calls rather
> than writing directly to memeory. The 'easy' solution is tempting, but
> in the long run, the discipline of using the 'right' approach pays
> off.

> Tom

I would like to point out the advisability of never using the BIOS unless
absolutely necessary - doing so will limit your program speed
unnecessarily.

Now that DOS is dead, there is little reason to code for future changes
in keyboard and display addressing.

It is easy to detect variations in a user's configuration (keyboard
extenders, mono displays, etc.) and have your code accommodate all normal
setups.

Some pathological setups dating back to the early days may or may not be
relevant to your application. If they are not, and there is no
chance they ever will be, there is no sense worrying about them.

There is plenty of information in SWAG on fast display writing, fast
keyboard addressing, and any other topic you may need. There have been
numerous posts in this newsgroup recently on this very topic - search
DejaNews for the current information in the area you are interested in.

It is ludicrous to accept the speed penalty and software restrictions of
using BIOS when you can have reliable routines that do what you want, the
way you want.

IMHO

Mike

Other Threads