Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

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

mtoromtoro Posts: 2
edited January 2012 in OpenSesame

Hello!
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?

Thanks,

Good Look!

Comments

  • sebastiaansebastiaan Posts: 2,812
    edited 9:59AM

    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()
                break
    
    key_up_time = None
    while key_up_time == None:
        for event in pygame.event.get():
            if event.type == KEYUP:
                key_up_time = self.time()
                break
        
    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:
    http://www.pygame.org/docs/ref/event.html

    For doing the same trick with the psychopy back-end, please refer to the psychopy documentation:
    http://www.psychopy.org/api/event.html

    Hope this helps!

    Kindest regards,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • mtoromtoro Posts: 2
    edited 9:59AM

    Many Thanks for the Answer!!

    Will try it monday and comment on the results,

    Great Job for OS,

    Hope you have a good time!

    Mauricio.

  • sebastiaansebastiaan Posts: 2,812
    edited 9:59AM

    Inactive, marking as solved.

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

Sign In or Register to comment.