PDA

View Full Version : detecting two keys at once using getch,switch,case



Unregistered
05-10-2002, 10:04 PM
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

Magos
05-11-2002, 12:23 PM
http://www.cprogramming.com/cboard/showthread.php?s=&threadid=17505

muttski
05-11-2002, 02:12 PM
I dont believe its possible using getch, gonna have to hack some keyboard buffer **** up in assembly, or you could download allegro and use that for everything you could ever need.

btw magos, he obviously didnt get an answer with that other post so he tried again, the dumbass who answered it didnt know what he was talking about (or maybey I dont).

Unregistered
05-11-2002, 07:28 PM
Hey muttski!!

To do multiple key presses you will have to write an interrupt handler for interrupt 09h. The default interrupt handler installed by the BIOS does not allow for multiple keystrokes. It places them in a circular buffer and then reads from the buffer. If the buffer overflows, it will beep. This is fine for normal use but absolutely horrible for games.



interrupt (*OldInt9Handler)(...) //use ... for C++ programs

interrupt MyInt9Handler(...)
{
int keyhit=inportb(0x60);
if(keyhit & 0x80)
{
keyhit-=128;
}
switch Keys[keyhit]
{
case 1: Keys[keyhit]=0;break;
case 0: Keys[keyhit]=1;break;
}

}


To track them now:


if (Keys[scancode])
{
//key with scancode has been pressed
}

You cant use a switch statement because switch is for mutually exclusive items. In other words, after you find one item to be true, you do a break which then exits the switch altogether. If you don't break, then all of the next statements will execute until a break is encountered.

To install it:


OldInt9Handler=getvect(0x09); //save current vector
setvect(0x09,MyInt9Handler); //install new one


To restore the IVT:


setvect(0x09,OldInt9Handler); //restore vector to old one

VirtualAce
05-11-2002, 07:30 PM
...that Unregistered moron was Bubba...

Note to self: To increase post counts, one must first login

:D

muttski
05-11-2002, 07:43 PM
This guy (bubba) knows what hes doing.

Im kinda confused on this part:


if(keyhit & 0x80)
{
keyhit-=128;
}

so your testing to see if that last bit is set, but why then clear that last bit?

Unregistered
05-11-2002, 08:50 PM
im kinda confused by the whole thing seeing that i dont know how to use interrup, 0x09(or other #'s), and setvector

now im not asking for you to explain those, but could you post the url of a free tutorial that explains them?

muttski
05-13-2002, 10:35 AM
Hmmmm, ok heres what you do, go download djgpp and allegro.

muttski
05-13-2002, 12:38 PM
Can someone answer my question, why clear that bit?

VirtualAce
05-14-2002, 11:54 PM
You clear the bit because when a key is held down the port returns the key+128 or 0x80. So to make it just toggle a flag on and off, you subtract 128 from the held down value. Otherwise you will never toggle the correct key flag, you will only be placing ones at key+128 and never clearing the flag at the actual key scan code in the array.