Howdy, Stranger!

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

Supported by

[solved] Collecting number of keypresses in a fixed amount of time

edited March 2014 in OpenSesame

Hi, i'm making an experiment and i want to collect the number of times that the subject press the key "1" in 8000ms.
That's the important part.

If possible (it's secondary), i want to collect also the times between responses, or the amount of time if "1" is being holded (it would be interesting for a graphic, later).

It seems simple, but i'm new with this software and i would be very grateful if somebody can help me, because we're a little rushed.

Thanks, thank you for make OpenSesame, great software.

Comments

  • edited March 2014

    Hi!

    In theory, you could do this using the GUI, but you'd end up with massive data files! The easiest solution would probably be to use the following code in an inline_script item:

    # import the keyboard class, to be able to
    # poll the keyboard
    from openexp.keyboard import keyboard
    # import numpy, which we will use to do
    # some calculations with
    import numpy
    
    # initialize a keyboard instance
    kb = keyboard(exp, keylist=['1'], timeout=10)
    
    # create an empty list, to store the keypress
    # times in (so we can use those later)
    presstimes = []
    
    # let the user smash your keyboard for 8 seconds!
    t0 = self.time()
    while self.time() - t0 < 8000:
        # wait for a keypress
        keypress, presstime = kb.get_key()
        # add the press time to our list
        if keypress:
            presstimes.append(presstime)
    
    # convert the list of keypresses to a numpy
    # array of keypresses, to make calculations
    # easier to do
    presstimes = numpy.array(presstimes)
    
    # after the frenzy, convert the keypress times to
    # reactiontimes, by calculating the difference
    # between each presstime (using numpy.diff)
    intresptimes = numpy.diff(presstimes)
    
    # calculate the mean and standard deviation
    # of the inter-press times
    M = numpy.mean(intresptimes)
    SD = numpy.std(intresptimes)
    
    # some more descriptives: the amount of
    # presses and the standard error of the mean
    N = len(intresptimes)
    SEM = SD / (N**0.5)
    
    # save the variables so that they can be used
    # in OpenSesame's GUI
    exp.set("Nipt", N)
    exp.set("Mipt", M)
    exp.set("SDipt", SD)
    exp.set("SEMipt", SEM)
    

    Keeping track of how long the button was down, is not possible in this way. You'll have to directly use either PyGame or PsychoPy to do so.

    Good luck!

  • edited 3:58AM

    Thank you very much Edwin!

    I'm experiencing this problem when trying to run the experiment with the given code:

    Error while executing inline script
    
    phase: run
    item: inline_script
    line: 9
    exception message: __init__() takes at least 2 arguments (3 given)
    exception type: TypeError
    
  • edited March 2014

    My apologies, that's due to a silly mistake (forgot to pass exp to the keyboard!). I've edited my previous post, so it should work now.

  • edited March 2014

    Thank you very much. It works like a charm.

Sign In or Register to comment.