KeyPressed() record and replay

How can I get all Keys Pressed during an specific amount of time and replay the same exact order keys were pressed using a loop. This need to be triggered by another key.

Hi,
I’ve played a bit with it and came up with the following:
In ofApp.h

struct keyPressedData{
        keyPressedData(uint64_t time, int key){
            this->time = time;
            this->key = key;
        }
        uint64_t time; // ofGetElapsedTimeMillis returns uint64_t
        int key;
    };
    vector<keyPressedData> keyEvents;
    bool bRecord = false;
    bool bPlay = false;
    uint64_t startTime = 0;
    uint64_t startPlayTime = 0;
    int keyEventReadPos = 0;

And if ofApp.cpp change update() and keyPressed(int key) to:

void ofApp::update(){
    if(bPlay){
        while(keyEvents[keyEventReadPos].time + startPlayTime < ofGetElapsedTimeMillis()){
            cout << "Play " << (char)keyEvents[keyEventReadPos].key << endl;
            keyEventReadPos++;
            if(keyEventReadPos >= keyEvents.size()){
                cout << "Done playing all the keys" << endl;
                bPlay = false;
            }
        }
    }
}
void ofApp::keyPressed(int key){
    if(key == 'r'){
        bRecord = true;
        bPlay = false;
        keyEventReadPos = 0;
        keyEvents.clear();
        startTime = ofGetElapsedTimeMillis();
        cout << "Start recording" << endl;
    } else if(key == 's'){
        bRecord = false;
        cout << "Stop recording" << endl;
    } else if(key == 'p'){
        cout << "Start playing" << endl;
        bPlay = true;
        startPlayTime = ofGetElapsedTimeMillis();
    } else {
        if(bRecord){
            uint64_t relativeStartTime = ofGetElapsedTimeMillis() - startTime;
            keyEvents.push_back(keyPressedData(relativeStartTime, key));
            cout << "Record " << (char)key << " at " << relativeStartTime << endl;
        }
    }
}

And in update() you should change
cout << "Play " << (char)keyEvents[keyEventReadPos].key << endl;
to
keyPressed(keyEvents[keyEventReadPos].key);
Not sure what whill happen when you record the ‘s’, ‘r’ or ‘p’ key. But probably something bad :wink: