Game Development Community

Detecting 2 keys input at the same time

by Alexander de Hartdegen · in Torque Game Builder · 02/02/2009 (9:46 pm) · 1 replies

How do i detect 2 keys input at the same time? for example, pressing the key "z" and "x" together.

Any advice would be appreciated.
Thanks

#1
02/03/2009 (5:03 am)
The 'fish game' tutorial has an example of this - but I guess it depends how you handle your key press events.

Anyway, the basic concept is when the keypress event occurs, you set a flag. Then you run your update code on a timer (or in a loop, or whatever you are using). In this update function you check this keypress flag. If you press both keys at the same time, and the interval between loops is long enough, then your flag should indicate that both 'z' and 'x' were pressed and you can run the appropriate code.

function fishPlayerUp()
{
   $FishPlayer.moveUp = true;
   $FishPlayer.updateMovement();
}

...

function PlayerFish::updateMovement(%this)
{
   ...

   if(%this.moveUp)
   {
      %this.setLinearVelocityY( -$FishPlayer.vSpeed );
   }

   ...
}

In any case, check out step 4 ('Adjusting Movement') of the 'fish game' tutorial.

Hope that helps,
Sam.