How to record keyboard or controller input?

Hi,
I am making a racing game and i need to make the replay mode.
Is there a script to record and save the input from the keyboard or controller so it can be read later?
I am using Upbge 0.2.1.

better yet, record position and orientation to a list.

notice that vectors and matrices are classes, and need to be converted to a tuple or list before assigned to the globalDict.

r = 3 #round digits
trf = owner.worldTransform
trf = (
    trf[0].to_tuple(r),
    trf[1].to_tuple(r),
    trf[2].to_tuple(r),
    trf[3].to_tuple(r)
)

if "REPLAY" not in logic.globalDict:
    logic.globalDict["REPLAY"] = []

logic.globalDict["REPLAY"].append(trf)