combined keys don't work

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
PixelBender
Posts: 7
Joined: Thu Oct 20, 2016 8:30 am

combined keys don't work

Post by PixelBender »

Hi all,

I'm trying to vary the speed of the FPS-Camera.
With Arrow keys only the speed should be 0.1.
And if additionally the shift key is pressed, the speed should be 1.

Therefore I reused and modified the code of the example 09.Meshviewer.
I added the following event handler:

Code: Select all

    /*
        Handle key-down events
    */
    bool OnKeyDown(irr::EKEY_CODE keyCode)
    {
        // Don't handle keys if we have a modal dialog open as it would lead 
        // to unexpected application behavior for the user.
        if ( hasModalDialog() )
            return false;
        
        if ((keyCode == irr::KEY_RSHIFT) && (keyCode == irr::KEY_UP))
        {
             MovingSpeed = 1;
        }
    }
unfortunately the program pointer never enters the part with MovingSpeed = 1;

What am I doing wrong?

Greetings
Markus
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: combined keys don't work

Post by CuteAlien »

You get one event per key-press, so OnKeyDown is called twice - once for KEY_RSHIFT and once for KEY_UP. You can't get them together.
There are 2 ways around that. First is that SEvent::SKeyInput has a variable 'Shift' which is set when the key was pressed while shift-key was pressed the same time. Also 'Control' for control key.

If you need other key-combinations then write all key-inputs into a boolean array of size KEY_KEY_CODES_COUNT. Set the value when the key is pressed and clear it when it's released. That way you can check the current value of each key all the time by checking that array.
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