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!
How to check to see if key is held?
-
DarkDepths
- Posts: 126
- Joined: Sun Apr 02, 2006 1:21 am
- Location: Canada
Look here:
http://irrlicht.sourceforge.net/docu/st ... input.html
...the key input struct has a PressedDown attribute.
http://irrlicht.sourceforge.net/docu/st ... input.html
...the key input struct has a PressedDown attribute.
-
DarkDepths
- Posts: 126
- Joined: Sun Apr 02, 2006 1:21 am
- Location: Canada
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.
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.
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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.
Then in your app code, you can just check the states each time round your main loop:
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;
}
};
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
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


