-1

I'm trying to figure out the best way to timeout the input if too much time passes between key presses. Should I create a cheatTime float that's incremented by delta every call to update? It's late, I'm tired and my brain is fried. Any help would be appreciated. m_pDI is a DirectInput wrapper object. Inside that class I have a ClearInput() function which will clear out any input inside its buffer.

if( m_pDI->KeyDown( DIK_TAB ) && m_pDI->KeyDown( DIK_1 ) )
    {
        if( m_pDI->KeyDown( DIK_H ) )
        {
            m_bGameCheats[ 0 ] = true;
            if( !m_pXA->SFXIsSoundPlaying( m_nCheatSoundID ) )
                m_pXA->SFXPlaySound( m_nCheatSoundID, false );
        }
        else if( m_pDI->KeyDown( DIK_C ) )
        {
            m_bGameCheats[ 1 ] = true;
            if( !m_pXA->SFXIsSoundPlaying( m_nCheatSoundID ) )
                m_pXA->SFXPlaySound( m_nCheatSoundID, false );
        }
        else if( m_pDI->KeyDown( DIK_S ) )
        {
            m_bGameCheats[ 2 ] = true;
            if( !m_pXA->SFXIsSoundPlaying( m_nCheatSoundID ) )
                m_pXA->SFXPlaySound( m_nCheatSoundID, false );
        }
    }
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
Josh Lake
  • 567
  • 1
  • 6
  • 17

1 Answers1

0

Please tell me if I got the question right. Two scenarios:

1: You only want to do something when B is pressed after A is pressed, unless the time that passes between the 2 is too long.

In that case store the time whenever A is pressed. When B is pressed check the time at which A was last pressed.

2: You want to do one thing if when B is pressed after A is pressed, and something else if B is not pressed after A is pressed.

Since you only know that B is not pressed after waiting for a timeout after A has been pressed, you need to start a timeout as soon as A is pressed and restart it whenever A is pressed again. The implementation may be event based, based on a periodic theck if you have a timeframe-based run loop, or a busy polling loop if the application/thread doesn't do much.

The polling looks something like this:

void AIsPressed()
{
    mLastTimeAWasPressed = clock();
}

bool HasTooMuchTimePassedSinceAWasPressed()
{
    return (clock() - mLastTimeAWasPressed) > (mTimeout * CLOCKS_PER_SEC)
}
Peter
  • 5,608
  • 1
  • 24
  • 43