Key event problems...

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Key event problems...

Post by Stu L Tissimus »

You know how in the Nintendo 64 Zelda games, you can lock on to enemies by holding down a button? (Or, if you don't, basically you press and hold a button and your focus goes to the enemy closest to you.) I'd like to replicate that in Irrlicht. I'm just using the demo as my base for the game. Now, I already have the basic code in:

Code: Select all

	if (event.EventType == EET_KEY_INPUT_EVENT &&
		event.KeyInput.Key == KEY_KEY_E &&
		event.KeyInput.PressedDown == true)
	{
		OutputDebugString("You just pressed the lock-on key.\n");
			device->getSceneManager()->getActiveCamera()->setTarget(model2->getPosition());
	}
Now, this works perfectly; as long as I'm not pressing any of the movement buttons. Then it doesn't recognize it. Is there any way for me to have both the locking-on key and a movement key (WASD) get recognized at the same time? Does it have to do with the fact that the WASD keys are in the currentScene switch and not in OnEvent? Here's my (read: demo code + one change) OnEvent code in its' entirety:

Code: Select all

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();
	}
	else
	if ((event.EventType == EET_KEY_INPUT_EVENT &&
		event.KeyInput.Key == KEY_SPACE &&
		event.KeyInput.PressedDown == false) ||
		(event.EventType == EET_MOUSE_INPUT_EVENT &&
		event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) &&
		currentScene == 3)
	{
		// shoot 
		shoot();
	}
	else
	if (event.EventType == EET_KEY_INPUT_EVENT &&
		event.KeyInput.Key == KEY_KEY_E &&
		event.KeyInput.PressedDown == true)
	{
		OutputDebugString("You just pressed the lock-on key.\n");
			device->getSceneManager()->getActiveCamera()->setTarget(model2->getPosition());
	}


	else
#ifdef _DEBUG
	if (event.EventType == irr::EET_MOUSE_INPUT_EVENT &&
		event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
	{
		char tmp[255];
		core::vector3df pos = device->getSceneManager()->getActiveCamera()->getAbsolutePosition();
		sprintf(tmp, "points.push_back(core::vector3df(%ff, %ff, %ff)); // %d\n", pos.X, pos.Y, pos.Z,
			sceneStartTime - device->getTimer()->getTime());
		OutputDebugString(tmp);
		return true;
	}
	else
#endif
	if (device->getSceneManager()->getActiveCamera())
	{
		device->getSceneManager()->getActiveCamera()->OnEvent(event);
		return true;
	}

	return false;
}
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
Stu(Lazy)

Post by Stu(Lazy) »

Also, another thing: When you press and hold E, it will lock on for a second, then stop locking on, then start again. I don't know the way it is in OSX, and I don't know if Linux/X11 does this, but in Windows, open up a text editor and hold down any character key. H, F, O, anything. See how it pauses for a second before repeating that letter? That's what is happening to me in the game.
Guest

Post by Guest »

Your problem is that you're not checking key state but key changes.
You only get an event when a key is pressed or released, not in between.
If you are getting repeating presses, that's because you also get releases before them.

Go to the FAQ forum. Look for the "bool keys[]" thread. The code isn't perfect (the updown and downup code doesn't work the way it should) but it'll get you started.
Post Reply