Aquiring input...

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
Taymo
Posts: 55
Joined: Mon May 22, 2006 1:06 am
Location: Colorado Springs - CTU
Contact:

Aquiring input...

Post by Taymo »

I am having trouble probing when two keys are pressed down. In my event receiver I tell my controls system that a key has been pressed. Then the controls system pushes that key back into a vector. It will keep pushing keys back until the main character updates (when I'm checking input).

The problem is that if I hold two keys, it doesn't seem to load both of them. If two keys are down does OnEvent get called twice for both keys? That's what I was expecting, because then both keys are pushed back before the update, and I can handle both. If you need more info or code snippets, request them, but I would just like to know how you guys do it. Thanks.
Ride the Spiral Development:
spiralride.blogspot.com
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

This has been asked hundreds of times on these forums. Search for bool keys.

Essentially you need to have a set of flags that indicate the state of each key either pressed down or not. Then when you check input you check the state of the key combinations you want to handle.

Travis
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Aquiring input...

Post by randomMesh »

rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

One way of doing it. This implementation also stores mouse button states (in a slightly different way than key states).

Code: Select all

class CMyEventReceiver : public IEventReceiver
{
public:

    typedef enum { LeftButton, MiddleButton, RightButton } EButton;

    // This is the one method that we have to implement
    virtual bool OnEvent(const SEvent& event)
    {
        // Remember whether each key is down or up
        if (event.EventType == EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
                case EMIE_LMOUSE_PRESSED_DOWN:
                    ButtonIsDown[LeftButton] = true;
                    ButtonClickPosition[LeftButton].X = event.MouseInput.X;
                    ButtonClickPosition[LeftButton].Y = event.MouseInput.Y;
                    break;

		        case EMIE_RMOUSE_PRESSED_DOWN:
                    ButtonIsDown[RightButton] = true;
                    ButtonClickPosition[RightButton].X = event.MouseInput.X;
                    ButtonClickPosition[RightButton].Y = event.MouseInput.Y;
                    break;

		        case EMIE_MMOUSE_PRESSED_DOWN:
                    ButtonIsDown[MiddleButton] = true;
                    ButtonClickPosition[MiddleButton].X = event.MouseInput.X;
                    ButtonClickPosition[MiddleButton].Y = event.MouseInput.Y;
                    break;

                case EMIE_MOUSE_WHEEL:
                    LastWheelMovement = event.MouseInput.Wheel;
                    break;
            }
        }

        return false;
    }

    // This is used to check whether a key is being held down
    bool isKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }

    //! Clears the button clicked state after doing the test.
    bool wasButtonClicked(EButton button, position2di & position)
    {
        if(ButtonIsDown[button])
        {
            ButtonIsDown[button] = false;
            position = ButtonClickPosition[button];
            return true;
        }

        return false;
    }

    f32 getLastWheelMovement(void)
    {
        f32 lastWheelMovement = LastWheelMovement;
        LastWheelMovement = 0.f;
        return lastWheelMovement;
    }


    CMyEventReceiver(void)
    {
        (void)memset(KeyIsDown, false, sizeof(KeyIsDown));
        (void)memset(ButtonIsDown, false, sizeof(ButtonIsDown));
        (void)memset(ButtonClickPosition, false, sizeof(ButtonClickPosition));
        LastWheelMovement = 0.f;
    }

private:
    bool KeyIsDown[KEY_KEY_CODES_COUNT];

    bool ButtonIsDown[3];
    position2di ButtonClickPosition[3];

    f32 LastWheelMovement;
  
};
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Taymo
Posts: 55
Joined: Mon May 22, 2006 1:06 am
Location: Colorado Springs - CTU
Contact:

Post by Taymo »

vitek wrote:This has been asked hundreds of times on these forums. Search for bool keys.

Essentially you need to have a set of flags that indicate the state of each key either pressed down or not. Then when you check input you check the state of the key combinations you want to handle.

Travis
Of course, that's what I meant by this:
Taymo wrote: In my event receiver I tell my controls system that a key has been pressed. Then the controls system pushes that key back into a vector. It will keep pushing keys back until the main character updates (when I'm checking input).
I'm just using a vector instead of an array...
So... I'm already using this system. I save the keystrokes until the update occurs. What seems to be the problem is that it does not grab two keys before my update occurs. So say I have two keys down at once.. will OnEvent be called twice separately for both keys? If not then I don't know what to do.. That would have to be the case for rogerborgs' example to work.
@randomMesh: Beautifully ignorant..
Ride the Spiral Development:
spiralride.blogspot.com
dudMaN
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Post by dudMaN »

The bool keys method is great and fixes this for some reason, there's an implementation in the tut in my sig.

Good luck :)

-DudMan
Complete Irrlicht Beginners Tutorial
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24898
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Taymo wrote:@randomMesh: Beautifully ignorant..
Uhm, sorry... But i think it's ignorant not to search the forums before posting.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

will OnEvent be called twice separately for both keys?
Yes, its called for every event occurred. This include keyboard, mouse, GUI as well as custom and log events.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Taymo wrote:What seems to be the problem is that it does not grab two keys before my update occurs. So say I have two keys down at once.. will OnEvent be called twice separately for both keys?
Yes, and no.

When you press a key down, you will get one (1) (PressedDown = true) event for that key. If you then hold it down, you will get further (PressedDown = true) events, but only when your OS decides to send them to Irrlicht, i.e. there will be a delay after the first event, followed by repeated events, exactly as you'd get when holding a key down in a text editorrrrrrrrrrrr.

You will then get one (PressedDown = false) event when the key is released.

In almost all cases, what you are interested in is the key state, not the events. A key's state is determined simply by considering the last event that was received for it. Unless you're implementing a text editor, there's no need to queue events; just set a boolean state for each key based on the last event received for it, then query the state of the keys whenever you want to act on them.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Taymo
Posts: 55
Joined: Mon May 22, 2006 1:06 am
Location: Colorado Springs - CTU
Contact:

Post by Taymo »

Thanks everyone that was good.
Ride the Spiral Development:
spiralride.blogspot.com
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

randomMesh wrote:
Taymo wrote:@randomMesh: Beautifully ignorant..
Uhm, sorry... But i think it's ignorant not to search the forums before posting.
Excuse me, but I have to comment on this. It is easy to through someone to the search function, but it is not always easy to utilize it. Take the following example, Taymo experiencing the second one:

1. You know what Deferred Shading is, but you want to see if it has been implemented with Irrlicht before. So you have a pretty solid understanding of what you will want to search for.

2. You want to know if anybody has rendered all the passes to a seperate texture, etc., but you don't know what it is called. How are you supposed to search for this?

So, in conclusion, I would say that it is best if you would consider yourself to be in the position of the person asking the question, and query yourself as to whether you would know what to search for. Now vitek did the right thing, in my mind, and simply told him what to search for considering he understood that you can't manifest a good search line from not even knowing what exactly you are searching for.
TheQuestion = 2B || !2B
Post Reply