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.
CodePudding user response:
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.
CodePudding user response:
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()))