How to creat a user input delay ,but record the accurate user press key?

I was wondering how to create 1 - 2 seconds delay between user input and game executive. My game is about a drunk man, I need a special effect : user’s input will take effect after 1-2 seconds. but every press key will be execute accurately. I guess I need key buffer to record the user input keys. but how can I record the interval time between the keys. Any suggestion?

You can grab the game time when the input happened, then add a couple of seconds to that to indicate some time in the future. When adding the input to your buffer just tag it with that value. Then when playing back the buffer you don’t do anything until you reach the scheduled time.

Here’s some mostly pseudo-code to kind of describe the concept…

// call this to check input and add it to the buffer
void BufferInput()
    // get the actual game time when the input happened
    float inputTime = Time.time;

    // schedule it for the future
    if (drunkMode)
      inputTime += DrunkDelayTime;  // set to 1-2 seconds, maybe randomly

    // add the input to the buffer, potentially scheduled for some time in the future
    QueueInput(... some representation of what the input is...., inputTime);

// call this to play back input from the buffer
void PlayBackInput()
    while (true)
      // let's take a look at the next queued input item...
      inputItem = PeekNextAvailableInputItem();

      // if the game time is now past the scheduled input time then it's time to execute it
      if (Time.time >= inputItem.ScheduledTime)
          // TODO : remove input from the queue
         // there is no input ready to go so move on

There are probably holes in my example, but it shows the basic idea: schedule the input for a later time, don’t execute the input until that time is past. The nice thing is that you can run all of your input through this, and if you don’t add anything to the scheduled time then it will just execute normally.

This should work pretty easily with keys. It might be a bit trickier with mouse input, but you basically just need to capture all of the relevant data about the input and queue it - which key was hit? Where was the mouse? etc.