Page 1 of 1

Simple problem with key pressed event

Posted: Tue Jan 05, 2010 8:52 pm
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

Posted: Tue Jan 05, 2010 10:56 pm
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.

Posted: Tue Jan 05, 2010 11:36 pm
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;
}

Posted: Wed Jan 06, 2010 12:14 am
by Mircea Popescu
I think you can supress the typematic rate of windows inside your application, I don't recall the exact setting tho.

Posted: Wed Jan 06, 2010 12:00 pm
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.

Posted: Wed Jan 06, 2010 8:38 pm
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.