Simultaneous Keyboard Input in C++

Started by
8 comments, last by Haplo 24 years, 4 months ago

It really depends on how you're doing your input right now. C++ by itself really doesn't have the keyboard functionality you need to do what you want, you're going to have to use some win32 calls (assuming you're writing this for windows)

Assuming you're doing your input by checking for a keypress, then reading the key by getch() or something similar, what you need to do is check the current state of the keys you're interested in.

try something like this :

if (GetAsyncKeyState(VK_UP) {
// handle UP arrow -- up for player 1
}
if (GetAsyncKeyState(VK_W)) {
// handle W key -- up for player 2
}

Hope that helps.

------------------
- Remnant
- (Steve Schmitt)

- Remnant- (Steve Schmitt)
Advertisement
Your teacher didn't know how to read multiple keypresses? Sad.
William Reiach - Human Extrodinaire

Marlene and Me


Gromit, for one - most programming teacher (notice I said MOST!) can only do stupid programming copy/pasted from a book. None of them do really difficult stuff.
If they knew how to do really good programming they'd get a programming job and make a lot more money.

Now on to answering the actual question - If you're writing this for DOS (which a fair amount of CS classes are still DOS-based) then you'll have to look at overriding the keyboard ISR, that involves some nasty 80x86 Assembly and other garbage that would take too long to explain here.
If you want a full explanation of keyboard ISRs and a good intro book for DOS game programming look for "Teach Yourself Game Programming in 21 Days" by Andre Lamothe. It'll take you longer than 21 days to read and fully understand probably, but it's well worth it.
However, DOS programming is becoming almost nonexistent in games, so you might as well make the move to Windows as soon as possible.
In windows simultaneous keyboard input is a cinch, and the general code is in a post above me.

However, if you want an explanation of how this code works, or an introduction to windows programming, or an intro to game programming in windows then I'd HIGHLY, and I mean HIGHLY suggest the book "Trick of the Windows Game Programming Books" by Andre Lamothe.
I barely knew what "Win32" meant when I got that book, and now I feel confident that I know DirectX, basic Win32 code (very basic, but enough for a game) and all kinds of cool stuff.

Anyway, that's my thoughts.
P.S. - one last note : If (when) you go to windows, use VC++, it has the least problems with DirectX and all the documentation on MSDN assumes you're using it (to my knowledge...)

------------------
gameguru4@yahoo.com

How many high school programming teachers are actually cs grads? (One high school in my town has a business teacher doubling as a programming teacher). I, fortunately, had one who knew a lot about programming. Most of what I'm currently going through in Structure/Abstraction in college, I learned in that class. I see a lot of 14 and 15 year olds in high school, posting for help and I wonder if they've actually attempted to talk to their programming teacher (if a good one exists at their high school) who most assuredly knows the basics of a few languages. So, in addition to going on-line or getting a book, 14/15 year-old newbies might also want to try their high school programming professor for some help.

BTW, help files rule!!!

joeG
This reminds me of my teacher. Only he is worse. If you're doing some graphics programming he thinks you're playing a game (the fool).
And another one: The computer besides me made a lot of noise. The teacher shouts to shut of the sound of my computer and stop playing games (sounds familiar?). Everyone was saying that it really wasn't my computer. It took him at least 5 minutes to understand (ARGH!).
My High school had Unisys Computers. With the track ball attached to the keyboard of course. And this was only five years ago.

I just figured that by now the all the crazy math teachers that got stuck teaching a programming class had all perished by now. Make way for the CS teacher that knows what an interupt is. (and maybe how to use one).

William Reiach - Human Extrodinaire

Marlene and Me


I can't speek for anywhere else in the world but in the UK when I was taking my A-Levels in computing the basic role of the teacher was to teach you the theory of the subject. We where taught reasonable Pascal and basic 80x8g asm but we were only examed on the easy bits of it. The practicle side of the course revolved around us designing, specing and finaly implementing a project of our own. We where alowed to program in anything (I used Turbo Pascal wor Windows 1.5, the good ol' days) but for this side of things the teacher took a back seat and allowed us to get on with it. His job was to help us do the research that we required rather than to teach us. I must say that I realy enjoyed this class.

I do know that my teacher was a capable programmer and could definatly have got a programming job if he wanted too, but he didn't he enjoyed his job as a teacher and was good at it, mind you he had come from a maths background too.

I'd be surprised if a high school could keep their programming teachers (if they were capable) much more if those teacehers were cs grads (maybe two years at the most).
joeG
OOOPS, by accident, I posted this post on the isometric view forum. Anyways, I'm a student in high school and I just finnished an independant project of mine for my Computer Science class. I learnt C++ and made Pong, however it's only one player because I don't know how to read in simultaneous keyboard input. The problem I ran into was that if player1 pushed up to move his paddle upwards and then player2 did the same, player2 would lock out player1 and only player2's paddle would move. I asked my teacher and searched the internet to find an example in C or C++ but I came up dry. I would really appreciate it if anyone could point me in the right direction because I really want to make the game as good as I can possibly make it.

[This message has been edited by Haplo (edited December 13, 1999).]

U know that is so much better than some of the uni lecturers in the Australian uni I attended.

NT hung. Someone suggested turning the computer off and on again.

The lecturer (Senior, mind u) asked, "How?"

This topic is closed to new replies.

Advertisement