Simple problem with key pressed event

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
L o
Posts: 44
Joined: Sun Mar 29, 2009 2:53 pm

Simple problem with key pressed event

Post by L o »

Hi,

I have a problem catching the key pressed event. My code:

Code: Select all

class GameplayReceiver : public IEventReceiver
{
        //...
        bool OnEvent(const SEvent& event)
        {
                if(event.EventType == irr::EET_KEY_INPUT_EVENT)
                {
                    if(event.KeyInput.PressedDown && event.KeyInput.Key == KEY_KEY_A)
                        puts("A PRESSED DOWN");
                    if(!event.KeyInput.PressedDown && event.KeyInput.Key == KEY_KEY_A)
                        puts("A RELEASED");
                }
        }
};
If the user keeps pushing the key down, I would expect the message "A PRESSED DOWN" to appear once, and then, nothing should come until the A key is relesed.

Instead, I get:

A PRESSED DOWN
A RELEASED
A PRESSED DOWN
A RELEASED
...

until I release the key. Is this really wanted or is it a bug I made?

Greets,
Lo
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Key-repeat is actually wanted. But I suppose this could still be improved some day by adding either a parameter to suppress repeat events or maybe add another flag to the events to mark them as repeats. You can add that to the feature-wish-tracker if you want. I guess it would be useful.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

yeah I would love that too :)

here's what I do to make sure a key press is triggered only once, but that way it triggers when you release the key not when you press it.

Code: Select all

bool CEvents::OnKeyInputEvent(const irr::SEvent &e)
{
  if (!e.KeyInput.PressedDown)
  {
    switch (e.KeyInput.Key)
    {
      case KEY_SPACE :
      {
        // do something
      } break;

      case KEY_ESCAPE :
      {
        device->closeDevice();
      } break;
    }
  }

  keys[e.KeyInput.Key] = e.KeyInput.PressedDown;
  return false;
}
Mircea Popescu
Posts: 26
Joined: Sat Jan 02, 2010 12:48 pm

Post by Mircea Popescu »

I think you can supress the typematic rate of windows inside your application, I don't recall the exact setting tho.
Image
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

I thought you're supposed to see:

Code: Select all

A PRESSED DOWN
A PRESSED DOWN
A PRESSED DOWN
...
A RELEASED
In this case you can detect the repeats if you want to (ie, for text input in your GUI) but also ignore multiple pressed down events if you're controlling a character.

So I could be wrong, but this may be a bug.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

bitplane wrote:I thought you're supposed to see:

Code: Select all

A PRESSED DOWN
A PRESSED DOWN
A PRESSED DOWN
...
A RELEASED
In this case you can detect the repeats if you want to (ie, for text input in your GUI) but also ignore multiple pressed down events if you're controlling a character.

So I could be wrong, but this may be a bug.
Yes, you are right. At least on Linux that's what you get - should be the same on Windows.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply