Jump to content
  • Advertisement
Sign in to follow this  
Nongan

Getting multiple key presses in C/C++ DOS compiler

This topic is 4983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was wondering how one would go about recieving multiple keypresses from the keyboard at once in Turbo C++ 3.1 (An older DOS compiler). I'm making a 2-player ASCII animated game where there a guys in the center that block your bullets and then there is player 1 at the bottom and player 2 at the top, and they have to shoot and kill each other. I want to have each player be able to shoot and move at the same time while the other player does the same. I have everything finished but that.

Share this post


Link to post
Share on other sites
Advertisement
Neither C nor C++ can do it by themselves - the language does not know anything about keyboards, only about files (standard input is one of them).

You need to rely on platform-specific functions. With Turbo C++ 3.1, the only thing I can think of would be for you to start digging into keyboard make and break codes. ASM may be involved.

Share this post


Link to post
Share on other sites
If it's a real DOS game, you need to insert a handler into the keyboard interrupt (9) and extract the keypressed yourself.
Basically you get the presses and releases sent; a pressed key has the 0x80 flag set. I gotta lookup exactly in my old (Turbo Pascal) code.

Do NOT forget to reset the interrupt handler once you're done with your game. Might not matter much when running from Windows but will matter in DOS.

Ok, found something: It's not my code, just found it from the net. It implements a nice handler. Shouldn't be too hard to replace with Turbo C functions.


var oldkbdint, oldtimint, oldbrkint: pointer;

procedure sti;
inline($fb); { STI: set interrupt flag }

procedure cli;
inline($fa); { CLI: clear interrupt flag -- not used }

procedure calloldint(sub: pointer);

{ calls old interrupt routine so that your programs don't deprive the computer
of any vital functions -- kudos to Stephen O'Brien and "Turbo Pascal 6.0:
The Complete Reference" for including this inline code on page 407 }

begin
inline($9c/ { PUSHF }
$ff/$5e/$06) { CALL DWORD PTR [BP+6] }
end;

procedure newkbdint; interrupt; { new keyboard handler }
begin
keydown[port[$60] mod 128] := (port[$60] < 128); { key is down if value of
60h is less than 128 --
record current status }
if port[$60] < 128 then wasdown[port[$60]] := true; { update WASDOWN if the
key is currently
depressed }
calloldint(oldkbdint); { call old interrupt }
mem[$0040:$001a] := mem[$0040:$001c]; { Clear keyboard buffer: the buffer
is a ring buffer, where the com-
puter keeps track of the location
of the next character in the buffer
end the final character in the
buffer. To clear the buffer, set
the two equal to each other. }
sti
end;

procedure initnewkeyint; { set new keyboard interrupt }
var keycnt: byte;
begin
for keycnt := 0 to 127 do begin { reset arrays to all "False" }
keydown[keycnt] := false;
wasdown[keycnt] := false
end;
getintvec($09, oldkbdint); { record location of old keyboard int }
setintvec($09, addr(newkbdint)); { this line installs the new interrupt }
sti
end;

procedure setoldkeyint; { reset old interrupt }
begin
setintvec($09, oldkbdint);
sti
end;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!