Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect and process multiple keys pressed at once?

Status
Not open for further replies.

EtienneDufresne

Programmer
Jul 12, 2007
3
CA
Hi,

I'm working on a personal project using Borland C++ Builder 6. This project consists of a plain form on which I draw a circle with a direction line and I make it move using the keyboard. I currently have some keys mapped to move the circle. I use the event called OnKeyDown on the main form to manage the key pressed.

'w' : Move the circle forward in the direction pointed by the line.
's' : Move the circle backward from the direction pointed by the line.
'a' : Rotates the circle Counter Clock Wise.
'd' : Rotates the circle Clock wise.

For the moment, if I press and hold down any of these keys the corresponding action is executed periodically which fine. (ex: pressing and holding down 'w' makes the circle moves forward on the form until I release the key) That means the OnKeyDown event is happens periodically if I hold down a key.

If I press any other key while I'm holding down 'w' the OnKeyDown event does not see it. I would like to be able to move the cicle forward while rotating CCW or CW. This would require to detect which 2 keys are pressed.

How can I detect that 'w' and 'd' are pressed at the same time?

Here is my OnKeyDown even handler code:

void __fastcall TFDisplay::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
if(Key == 'd' || Key == 'D')
{
Particule1->RotCCW();
}
if(Key == 'a' || Key == 'A')
{
Particule1->RotCW();
}
if(Key == 'w' || Key == 'W')
{
MoveForward();
}
if(Key == 's' || Key == 'S')
{
MoveBackward();
}
}

The TShiftState parameter checks the state of the Alt, Ctrl and Shift key. I'm looking to do the same thing with letters. (ex: if 'w' is held down and 'd' is pressed rotated CCW and move forward).

I looked in the online help of Borland for any information. I found something called:"IOTAKeyboardBinding::BindKeyboard" I read a bit about that but whenever I try to declare an instance of IOTAKeyboardBinding the compiler cannot find it.

Well that's about it, thank you very much for any help you may provide!

Etienne Dufresne
 
I think that you get here is how Windows itself processes keyboard events.
So to get past it you should go one layer deeper, probably Direct X.

(this is just my musings. I never touched Direct X in my life).

I did some search but found nothing of much use. May be you'll be more lucky.
Here two links that looks somewhat related:
 
Thank you tsh73 that seems to be what I was looking for. I looked in the online help of Borland Builder 6 and I got lots of info on this function. I'll give it a shot sometime this week and I'll let you know if it works. Thank you very much for your help!!!

Etienne
 
I added a timer to my form and inputed the following code in it and now everything works the way I want it too !!!!

//If 'D' is held down.
if((GetKeyState(0x44) & 0x0080) == 0x0080)
{
Particule1->RotCCW();
}
//If 'A' is held down.
if((GetKeyState(0x41) & 0x0080) == 0x0080)
{
Particule1->RotCW();
}
//If 'W' is held down.
if((GetKeyState(0x57) & 0x0080) == 0x0080)
{
MoveForward();
}
//If 'S' is held down.
if((GetKeyState(0x53) & 0x0080) == 0x0080)
{
MoveBackward();
}

Borland's online help is really weird I searched for hours to find a function that would send back the state of a key and I could not find anything. If you input GetKeyState in the index of the online help it does not find it. To get help on GetKeyState you have to write GetKeyState in your .cpp and press F1 while it's selected. Very strange...

Anyways it work now. Thank you for your help !!!

Etienne Dufresne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top