How do I record button presses and play them back?

I wondered whether the arduino could record a long sequence of button presses, and the amount of time they were pressed, then repeat the sequence at the press of a button.

The answer is yes, for some definitions of long and some number of buttons.

The number of buttons is limited by how you read them.

Each time a button release is detected, the button number and duration needs to be stored. If there are less than 255 keys, the key number can be stored in a byte. If the maximum time for a key to be help down is 32 seconds or less, the time can be stored in an int (2 bytes). Otherwise, you need to use a long (4 bytes).

So, from 3 to 6 bytes per press/release. With 2K of SRAM, and not using every last bit of it, you could store 300 to 600 button presses/times.

If you need to store more than that, you need some kind of external storage, such as an SD card.