There are a couple of ways to detect key presses.
The first and easier way is to watch the OnAction function of your map script for whenever the player initiates an action, e.g. eAction_Forward, eAction_Interact, eAction_Cancel (the eAction enumeration is in the "script/base/InputHandler_Types.hps" file if you want to see the full list of actions available). The benefit to this is that the key is automatically routed through the game's keybindings, so you can always detect a particular action regardless of what key it is bound to. The downside to this approach is that you are limited to only using existing actions in the game (though you
can add more if you know how).
To check against a particular action, you compare the alAction parameter against whichever action you are watching for:
bool OnAction(int alAction, bool abPressed)
{
// Check if the interact action was started (and if the key was pressed rather than released)
if (alAction == eAction_Interact && abPressed)
{
// Do stuff...
}
}
The other way is a bit more involved (and I haven't tested it yet myself, so I can't vouch for its effectiveness). You can get an instance of the
iKeyboard object and manually detect if certain keys are being pressed (using the
eKey enumeration). This has the benefit of allowing you to check the state of any key you want, but the downside of possible collisions with existing keybindings. You would also have to put it in your map's Update script if you wanted to continuously check for key presses which could potentially affect performance (though probably not by much).
void Update(float afTimeStep)
{
iKeyboard @keyboard = cInput_GetKeyboard();
// Check if the key is pressed (note that this will return true for as long as the key is held down)
if (keyboard.KeyIsDown(eKey_E))
{
// Do stuff...
}
}