Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] How to log the duration of a key press?

edited January 2012 in OpenSesame

I've trying OS for less than a week, done the tutorial and would like to start making my own experiments in this platform, I used to use the psychophysics toolbox for matlab, wich was good, but pretty complicated to run under linux....
My question is:
How can I log the duration of a keypress, not just when it was pressed but for how long?


Good Look!


  • edited 6:36PM

    Hi Mtoro,

    Welcome to the forum!

    Yes, that's possible. But you will need to refer directly to the libraries used by the back-end, because the openexp.keyboard module doesn't support "key up" functionality (I'll consider adding it, because it would be useful. So thanks for bringing this to my attention!)

    For the pygame-based back-ends (legacy and opengl) the following piece of inline_script should do the trick (so you would use an inline_script rather than a keyboard_response to collect responses):

    import pygame
    from pygame.locals import *
    key_down_time = None
    while key_down_time == None:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                key_down_time = self.time()
    key_up_time = None
    while key_up_time == None:
        for event in pygame.event.get():
            if event.type == KEYUP:
                key_up_time = self.time()
    key_duration = key_up_time - key_down_time
    self.experiment.set("key_duration", key_duration)

    You can modify this inline_script to fit your particular needs, but at least it shows how to determine the duration (uptime - downtime) for a keypress.

    For more information, you can refer to the pygame documentation:

    For doing the same trick with the psychopy back-end, please refer to the psychopy documentation:

    Hope this helps!

    Kindest regards,

  • edited 6:36PM

    Many Thanks for the Answer!!

    Will try it monday and comment on the results,

    Great Job for OS,

    Hope you have a good time!


  • edited 6:36PM

    Inactive, marking as solved.

Sign In or Register to comment.