Game Development Community

Keyboard bindings with modifier key

by Sir H. Appleby · in Torque Game Builder · 08/13/2008 (11:41 am) · 3 replies

Howdy all,

So I want the arrow key to do different things if the rshift key is being held down at the same time. I have had a look at the bindObj parameters but the only modifier I can see in the...somewhat erratic documentation refers to mouse stuff.

How would you go about changing this so that the arrow key can do different things depending on if rshift is held. I assume it involves modifying the bindObj method but I am not sure.

moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "rotationLeft", %this); // Should Rotate
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey,1) WITH 'rshift' MODIFIER, "lateralLeft", %this); // Should move laterally

Many thanks

Sir. H Appleby

-- EDIT

To add some info to my original post. This is what I have so far. Theoretically I think it should work but I have one problem. On each update I check, forward thrust, lateral thrust and rotational thrust.

When I press the left or right key the rotational or lateral value is changed depending on the shift key value. When the key is released both are set to 0 again. Because the behaviour is different on keypress and keyrelease I have used the binCmd to use separate functions.

Like I said, I think this should theoretically work. Unfortunately the bindCmd doesn't appear to be working. the key presses aren't being caught, any suggestions?

function NewtonianControlsBehavior::onBehaviorAdd(%this)
{
   if (!isObject(moveMap))
      return;
   
   %this.owner.enableUpdateCallback();
   %this.owner.setDamping(0);
   
   moveMap.bindObj(getWord(%this.upKey, 0),     getWord(%this.upKey, 1), "forwardThrust", %this);
   moveMap.bindObj(getWord(%this.downKey, 0),   getWord(%this.downKey, 1), "reverseThrust", %this);
   moveMap.bindCmd(getWord(%this.leftKey, 0),   getWord(%this.leftKey, 1), "pressLeft", "releaseLeft");
   moveMap.bindCmd(getWord(%this.rightKey, 0),   getWord(%this.rightKey, 1), "pressRight", "releaseRight");   
   moveMap.bindObj(keyboard,                    rshift,  "shift", %this);   
   
   %this.up = 0;
   %this.down = 0;
   %this.rotationleft = 0; 
   %this.rotationright = 0;
   %this.lateralleft = 0;
   %this.lateralright = 0;
   %this.shift = 0;  
}

function NewtonianControlsBehavior::onBehaviorRemove(%this)
{
   if (!isObject(moveMap))
      return;

   %this.owner.disableUpdateCallback();
   
   moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
   moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), %this);
   moveMap.unbindCmd(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
   moveMap.unbindCmd(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
   moveMap.unbindObj(keyboard, rshift, %this);  
   
   %this.up = 0;
   %this.down = 0;
   %this.rotationleft = 0; 
   %this.rotationright = 0;
   %this.lateralleft = 0;
   %this.lateralright = 0;
   %this.shift = 0;  
}

function NewtonianControlsBehavior::onUpdate(%this)
{
   %this.owner.setImpulseForcePolar(%this.owner.rotation, (%this.up - %this.down) * %this.forwardAcceleration);
   %this.owner.setImpulseForcePolar(%this.owner.rotation + 90, (%this.lateralright - %this.lateralleft) * %this.lateralAcceleration);   
   %this.owner.setAngularVelocity(%this.owner.getAngularVelocity += ((%this.rotationright - %this.rotationleft) * %this.rotationalAcceleration));   	   
   
}

function NewtonianControlsBehavior::forwardThrust(%this, %val)
{
   %this.up = %val;
} 

function NewtonianControlsBehavior::reverseThrust(%this, %val)
{
   %this.down = %val;
}

function NewtonianControlsBehavior::pressLeft()
{
   if (%this.shift == 0 )
   {
      %this.rotationleft = 1;
   }
   else
   {
      %this.lateralleft = 1;
   }
}

function NewtonianControlsBehavior::releaseLeft()
{
      %this.rotationleft = 0;
      %this.lateralleft = 0;
}

function NewtonianControlsBehavior::pressRight()
{
   if (%this.shift == 0 )
   {
      %this.rotationright = %val;
   }
   else
   {
      %this.lateralright = %val;
   }
}

function NewtonianControlsBehavior::releaseRight()
{
      %this.rotationright = 0;
      %this.lateralright = 0;
}

function NewtonianControlsBehavior::shift(%this, %val)
{
   %this.shift= %val;
}

#1
08/13/2008 (12:53 pm)
I do not believe that you can bind a modifier key by itself. You can do something like this..
bind( keyboard, "shift left", strafeLeft );
bind( keyboard, "left", turnLeft );
#2
08/13/2008 (12:56 pm)
Your bindCmd is not calling NetwonianControlsBehavior::pressLeft/etc because only bindObj will call functions within a namespace.
#3
08/13/2008 (1:15 pm)
Ta for the info.

Finally found the answer in the TDN. Simple when you know how...

moveMap.bindObj(getWord(%this.leftKey, 0), "shift " @ getWord(%this.leftKey, 1), "thrustLeft", %this);
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "rotateLeft", %this);