Page 1 of 1

OnEvent problem

Posted: Fri Oct 05, 2007 2:59 pm
by mironcaius
So i added some code to mode a node in the OnEvent function, but my code cannot accept events on the same time, like when i push up and then left, and i dont undestand what i am doing wrong.
Here is a sample of the code:

bool CDemo::OnEvent(SEvent event)
{
if (!device)
return false;

if (event.EventType == EET_KEY_INPUT_EVENT &&
event.KeyInput.Key == KEY_ESCAPE &&
event.KeyInput.PressedDown == false)
{
// user wants to quit.
if (currentScene < 3)
timeForThisScene = 0;
else
device->closeDevice();
}

if (model1 != 0 && event.EventType == EET_KEY_INPUT_EVENT && (event.KeyInput.Key == KEY_KEY_O || event.KeyInput.Key == KEY_KEY_K
|| event.KeyInput.Key == KEY_KEY_L || event.KeyInput.Key == KEY_KEY_M || event.KeyInput.Key == KEY_KEY_J) && event.KeyInput.PressedDown)
{

and then i use a switch:
switch(event.KeyInput.Key)
{
case KEY_KEY_O:
{

Posted: Fri Oct 05, 2007 3:21 pm
by vitek
You don't really post enough code to see what the problem is, but I can guess.

If you are pressing two keys simultaneously and the result isn't as expected, there are two possible problems that I can think of. You either don't have a way to indicate that multiple keypresses are happening simultaneously, or the code that handles the keypresses doesn't respond to both keys if pressed simultaneously.

For the first problem you most likely need to use what is often referred to here as bool keys. Basically you keep an array of bools that indicates the state of the keyboard keys, all of them. Your event receiver sets the key state for each key to true when the key is pressed and false when it is not. Now that you have the key states, you can check to see which key combinations are being used.

For the second part, you need to handle key combinations. This is most often a problem for movement. Here is an example of how you would do that...

Code: Select all

core::vector3df direction(0, 0, 0);
if (receiver.Keys[KEY_W])
    direction.Z += 1;
if (receiver.Keys[KEY_D])
    direction.Z -= 1;

if (receiver.Keys[KEY_A])
    direction.X += 1;
if (receiver.Keys[KEY_D])
    direction.X -= 1;

node->getAbsoluteTransformation().rotateVect(direction);
const core::vector3df offset = direction * elapsedSeconds;

const core::vector3df pos = node->getPosition();
node->setPosition(pos + offset);
Travis