How to check to see if key is held?

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
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

How to check to see if key is held?

Post by DarkDepths »

In the movement tutorial, pressing w makes the sphere move upwards. However, this only happens once per time the key is pressed. I would like to check to see if the key is being held, so that I can do smooth movement. It is rather imperative as I am trying to make a pong clone, and need the paddle movable.

Quick help would be best, but any would be great!
xray
Posts: 231
Joined: Fri Feb 02, 2007 1:06 pm
Location: Germany, Munich
Contact:

Post by xray »

Look here:

http://irrlicht.sourceforge.net/docu/st ... input.html

...the key input struct has a PressedDown attribute.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

You may also want to check the wiki for 'bool keys'.
Image Image Image
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Or search the forum for MastEventReceiver ...
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

I'm terribly sorry, but I can't get it to function properly. I managed only to make it move once while a key is pressed. Would it be possible for someone to write just the event receiver so that it has the function of holding up and down. Also, if you would do this huge favor for me, could you please comment it, or explain the code, so that I can understand what is going on! Thanks.

If no one is willing to do this for me, could you possibly try and explain how the event receiver works, because I really can't figure it out.
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

you can tron it, set a breakpoint in the code and run debug , you should be able to see what is going on when the ieventreceiver gets the control.
Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

The event receiver is sent one event, once, when a key is pressed or released, it doesn't get repeated "key down" events.

What you want to do is to store the last event (and therefore the state) for each key, then retrieve that state from inside your game loop.

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	
	// Create and expose an array of bools to store the key states
	bool KeyIsDown[KEY_KEY_CODES_COUNT]; // KEY_KEY_CODES_COUNT is defined in keycodes.h

	MyEventReceiver::MyEventReceiver()
	{
		// Ensure that all key states start as 'false'
		memset(KeyIsDown, false, sizeof(KeyIsDown));
	}

	virtual bool OnEvent(const SEvent& event)
	{
		// Remember the last PressedDown event for each key
		if (irr::EET_KEY_INPUT_EVENT == event.EventType)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		return false;
	}
};
Then in your app code, you can just check the states each time round your main loop:

Code: Select all

while(device->run())
{
    if(receiver.KeyIsDown[KEY_KEY_A])
        // do something appropriate

    // Rest of your main loop
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

Thank you. I got it too work now! Very much appreciated.
Post Reply