Howdy, Stranger!

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

Supported by

[solved] Key realease

edited January 2013 in OpenSesame

Hi
Does anyone know the best way to record a key release (rather than Press) in Opensesame? The participant will hold the response key (SPACEBAR) from the start of the trial until a certain event does or doesn't occur. I will need RTs recorded from the onset of an object (a new event) that will signify key release (or not) to the actual key release and also, from key release to a second mouse click (well, actually a touch screen input). Any help much appreciated

Comments

  • edited February 2014

    Hi JasT.

    I had the same need in the past, so I can show you my solution (which requires a piece of python code).

    I used pyglet KeyStateHandler function inside psycho backend, so I don't know if you can use the same function inside another backend. This is a simple code that you can adapt to your needs.

    from psychopy import visual, core
    from pyglet.window import key
    response = key.KeyStateHandler()
    self.experiment.window.winHandle.push_handlers(response)
    win = self.experiment.window
    time = core.Clock()
    press = visual.TextStim(win, pos=(0,0), text='Hold down the SPACEBAR...')
    release = visual.TextStim(win, pos=(0,0), text='When you want, release the SPACEBAR')
    # spacebar not pressed
    while not response[key.SPACE]:
        press.draw()
        win.flip()
    t1 = time.getTime() # spacebar pressed! (first timestamp)
    while response[key.SPACE]:
        release.draw()
        win.flip()
    t2 = time.getTime() # spacebar released! (second timestamp)
    spacebar_time = t2-t1 # compute the key pressing time
    

    I don't know if there is a simpler method. Hope this helps.

    Andrea

  • edited 6:42PM

    Thanks that's helpful - it makes sense to use a while loop to identify whether a key is no longer being pressed :\">

  • edited February 2014

    Btw, in case you're not using the psycho back-end, here's a small code snippet that illustrates how you can accomplish approximately the same thing with the PyGame-based back-ends (legacy, expyriment, and opengl):

    import pygame
    key = None
    start_time = self.time()
    while key == None:
        time = self.time()
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                key = event.key
    rt = time - start_time  
    print '%s was released after %d ms' % (key, rt)
    

    For more information, please see the PyGame documentation, and don't hesitate to post further questions!

    Cheers,
    Sebastiaan

  • edited 6:42PM

    Hi,

    I think this is a fantastic discussion; I have the same need for my study. I'm using sebastiaan's suggestion (for use with PyGame). However it doesn't seem to be recording the reaction time ("rt" variable). Or in fact, recording anything after the stimulus is presented. How do I get OpenSesame to record this part in logger?

    And...I'm a little uncertain how this works in the experiment workflow. Do I place this Python script after the keyboard_response item, before it, or does it entirely replace the keyboard_response item?

    Thanks,
    Adam

  • edited November 2014

    Never mind! Some more googling found me this comment, also by Sebastiaan.

    Basically, the python script needs to define the variable as well via exp.set() so there should also be a line like this:

    exp.set('keyreleaseRT', rt)

    All good now! Although I have no idea what the last print line is for, it doesn't seem to do anything.

  • edited November 2014

    HI Adam,

    All good now! Although I have no idea what the last print line is for, it doesn't seem to do anything.

    The timing for each keypress should appear there, since this is were prints usually print to.
    Anyway, good to hear that it works now for you.

    Best,

    Eduard

  • edited 6:42PM

    The timing for each keypress should appear there

    What does "there" mean? Is the screen supposed to display the print line after I release the key? I'm not sure how that works since there isn't a text_render item around that part of the loop.

    Thanks,
    Adam

  • edited 6:42PM

    Sorry! I think I rewrote my response and deleted something by accident. What "there" refers to is the debug window. If you print something, it does't appear on the screen, but the debug window.

    Sorry for the confusion.

    Eduard

  • edited 6:42PM

    Thank you, Eduard!

  • Hi,
    Sorry for resurrecting an old thread, but I was wondering if anyone had any information on how reliable the RT is using the method Andrea (Embodiment) proposed above. Would it be anywhere near millisecond accurate (ignoring issues tied to the particular keyboard itself)? Is it tied to screen refresh (the win.flip seems to suggest that, but I'm not sure)?
    Thanks,
    Mark

Sign In or Register to comment.