detecting two keys at once using getch,switch,case

• 05-10-2002
Unregistered
detecting two keys at once using getch,switch,case
i am making a game and i would appreciate it if anyone could tell me how to detect two keys being pressed at once impleminting the following code:

int Choice;
do
{
Choice=getch()
switch(Choice)
{
case 72: //up
Y--; //coords move up
}
putpixel(X,Y);
}while(Choice!=27); //27 is ascii for <Esc>

so how would i detect if the user inputs up and left at the same time, so that i could move the pixel up and left?
thanks for any help
• 05-11-2002
tyouk
Its been a very long time since I've had to do this, but I believe you can do it by checking for first the up key, and then check for either the left or right key. Then check for the down key and check for the right or left key with the following code:

int Choice;
int x,y;
do {
Choice = getch();
switch(Choice)
{
case 72:
Choice = getch();
// they want to go up and to the left
if(Choice == <place number for left key here>) {
y--;
x--;
}
// they want to go up and to the right
else if(Choice == <place number for right key here>) {
y--;
x++;
}
// they only want to go up
else {
y--;
}
break;
case <number for down key here>:
Choice = getch();
// they want to go down and to the left
if(Choice == <place number for left key here>) {
y++;
x--;
}
// they want to go down and to the right
else if(Choice == <place number for right key here>) {
y++;
x++;
}
// they only want to go down
else {
y++;
}
break;
}
putpixel(x, y);
}
while(Choice != 27);

And that should do it. Try it. If it doesn't work, then post a reply and I'll try another solution with you.