pythonpython-3.xpython-2.7psychopyopen-sesame

Is there a python code for tracking all keypresses (getting timeline for each keypress)?


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.


Solution

  • 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.