-
multiple keypresses
I was wondering....Lets say your making a game and the character can jump up in the air by pressing the Up arrow.
Then lets say the character can fly by holding down the "J" key...but to move forward, backward, etc. while flying, the user must press the arrow keys while holding down the "J" key.
How do you handle input of the user holding down more than one key at once? It is easy to check for a certain key being pressed in the main game loop, but how do you check for multiple keys being held down at one time?
Anybody have any experience with it? Please share....
-
I'm not sure if you've looked into DirectX or not, but DirectInput can easily handle multiple keypresses.
-
You didn't mention what compiler you're using. If you have access to <windows.h>, look into the GetAsyncKeyState() and other useful keyboard functions in MSDN:
Code:
while(GetAsyncKeyState(VK_J)) //if this is true
{
if(GetAsyncKeyState(VK_J) && GetAsyncKeyState(VK_UP))
{
flyForward(); //or up if its a side-scroller I guess
}
//and so on...
}
Hope this helps :)
-
this wont actually help you according to your question, but you could also do like -
player presses J and the fly-mode gets activated, if he then presses the arrow keys he flies to that direction. the next time he presses J the fly-mode gets deactivated and gets back to the ground and then if he uses the arrow keys he walks instead of flies.
i have used this once so thought about sharing it with you.
-
that's a great question! how do you do that, in DOS? :) [seriously...]
-
Code:
interrupt NewInt9Handler(...)
{
int keydown=inp(&h60);
if (keydown & 0x80)
{
keys[keydown-128]=0;
} else keys[keydown]=1;
//Send EOI to the PIC
outp(0x20,0x20);
}
if (keys[keyscancode])
{
//key is down
} else //key is released
This is for DOS only and does not check the current state of the keyboard. Different states will need to be masked out of the codes such as NumLock. This will not work when NumLock is on.
For Windows and MSVC, use DirectX.
-
That was Bubba up there by the way.
Cleared my cookies and forgot to sign in. Speaking of cookies...think I'll go and eat one.
:D
-
interrupt NewInt9Handler(...)
need any headerfile???
-
yeah bubba, could you post a sample program on how to use multiple keypresses?
-
wait a minute....I think I just figured it out...
By the way, I am doing this in SDL and using VC++...
But anyways, when checking for key presses, SDL lets you check for whether a certain key is either KEYDOWN or KEYUP. On KEYDOWN of the J key, I could activate fly mode....so fly mode would be activated while they hold down J, and on KEYUP, I deactivate it....
-
yeah i know allegro has similar constructs and i'm hoping that there are similar possibilities for this [i guess through interrupts] in DOS...