Why Key input event occurs twice?

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
keynet
Posts: 18
Joined: Wed Jun 27, 2012 9:07 am

Why Key input event occurs twice?

Post by keynet »

I'm using Irrlicht to make a simple 2D game.

Using IEventListener, I'm dealing with key input event so that 2D character can move.
Also, I fixed FPS as 30 FPS using GetTickCount() function, so I can get 30 inputs per second (one input for one frame).

However, I have a problem that irrlicht gets user's input twice event if i clicked a key just once, very carefully.
It happens for every key input; not just arrow key, but also other keys like space bar, etc.

Why is this situation happening? I really don't know with my knowledge.
From Seoul, South Korea
CuteAlien
Admin
Posts: 10035
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Why Key input event occurs twice?

Post by CuteAlien »

Once on pressing down and once on release? Or do you mean the key-repeat maybe? Otherwise hard to say without example.

Here's the code I often use for testing key-event behavior, maybe it helps:

Code: Select all

 
#include <irrlicht.h>
#include <iostream>
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
 
class MyEventReceiver : public IEventReceiver
{
    bool OnEvent( const SEvent &event )
    {
        if (event.EventType == EET_KEY_INPUT_EVENT) 
        {
            if( event.KeyInput.PressedDown )
            {
                std::cout << "PressedDown:" << event.KeyInput.Key << std::endl;
            }
            else if( !event.KeyInput.PressedDown )
            {
                std::cout << "!PressedDown:" << event.KeyInput.Key  << std::endl;
            }
        }
    
        return false;
    }
};
 
int main()
{
    video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
    IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
    if (device == 0)
        return 1; // could not create selected driver.
 
    video::IVideoDriver* driver = device->getVideoDriver();
    MyEventReceiver receiver;
    device->setEventReceiver(&receiver);
    
    while(device->run() && driver)
    {
        if (device->isWindowActive())
        {
            //          device->sleep(1000);
            //
        }
    }
 
    device->drop();
 
    return 0;
}
 
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
keynet
Posts: 18
Joined: Wed Jun 27, 2012 9:07 am

Re: Why Key input event occurs twice?

Post by keynet »

Oh I solved it. My problem was that it checks user's key input and move character in OnEvent Function.

After I moved the MoveCharacter() function in while(dev->run()), it gets exactly one input!

Thank you
From Seoul, South Korea
Post Reply