I am using OpenSesame (which runs on Python) for creating an experiment. I need a code for getting a timeline for each keypress (1, 2, 3, 4, space). For example, key 1 pressed at 12223ms, key 3 at 15452ms, key 1 again at 19112ms. Here, I need to keep track of all these keypresses (11223ms, 15452ms, 19112ms, etc.) from the beginning of the experiment.
Asked
Active
Viewed 107 times
-2
-
Welcome to StackOverflow, please add code you tried and the full error so we can help. – BrainFl Mar 07 '22 at 05:36
2 Answers
2
For tracking time, you can use the time
module. The time.time()
function gives you the current unix Timestamp (seconds since 1970). Take the time once at program start, then substract the "start" time from all other measurements to get the difference.
Detecting keypresses depends on the operating system you're on. On Windows, there is the msvcrt
module. Specifically, see msvcrt.getch.
For *nix there is a different library, can't remember its name right now. Google is your friend :-)
Edit: Apparently, OpenSesame has facilities for keyboard handling, so preferably use those.

Torben Klein
- 2,943
- 1
- 19
- 24
0
With datetime
from datetime import datetime
def startms(t):
# *1000 for ms
return (t-start).total_seconds()*1000
start = datetime.now()
# Your function here, or time.sleep for an example
print(startms(datetime.now()))

Freddy Mcloughlan
- 4,129
- 1
- 13
- 29