startrcewww-data endrce
Auto Repeat Key Input         A small class to take key presses and auto repeat keyboard input for GUI

AutoRepeatKey.h

#pragma once

#include <OIS.h>

class AutoRepeatKey {
private:

    OIS::KeyCode m_nKey;
    unsigned int m_nChar;

    float m_fElapsed;
    float m_fDelay;

    float m_fRepeatDelay;
    float m_fInitialDelay;

protected:

    virtual void repeatKey(OIS::KeyCode a_nKey, unsigned int a_nChar) = 0;

public:

    AutoRepeatKey(float a_fRepeatDelay = 0.035f, float a_fInitialDelay = 0.300f);

    void begin(const OIS::KeyEvent &evt);

    void end(const OIS::KeyEvent &evt);

    void update(float a_fElapsed); // Elapsed time in seconds
};

AutoRepeatKey.cpp

#include "AutoRepeatKey.h"

AutoRepeatKey::AutoRepeatKey(float a_fRepeatDelay, float a_fInitialDelay):
    m_nKey(OIS::KC_UNASSIGNED),
    m_fRepeatDelay(a_fRepeatDelay),
    m_fInitialDelay(a_fInitialDelay)
{}

void AutoRepeatKey::begin(const OIS::KeyEvent &evt) {
    m_nKey = evt.key;
    m_nChar = evt.text;

    m_fElapsed = 0;
    m_fDelay = m_fInitialDelay;
}

void AutoRepeatKey::end(const OIS::KeyEvent &evt) {
    if (m_nKey != evt.key) return;

    m_nKey = OIS::KC_UNASSIGNED;
}

void AutoRepeatKey::update(float a_fElapsed) {
    if (m_nKey == OIS::KC_UNASSIGNED) return;

    m_fElapsed += a_fElapsed;
    if (m_fElapsed < m_fDelay) return;

    m_fElapsed -= m_fDelay;
    m_fDelay = m_fRepeatDelay;

    do {
        repeatKey(m_nKey, m_nChar);

        m_fElapsed -= m_fRepeatDelay;
    } while (m_fElapsed >= m_fRepeatDelay);

    m_fElapsed = 0;
}