Howdy, Stranger!

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

Supported by

[solved] set duration as either mouseclick or keypress

edited November 2011 in OpenSesame

Is there a way to set the duration of a sketchpad as either keypress or mouseclick whichever comes first?

Thanks in advance
Ezer

Comments

  • edited 7:01AM

    Hi Ezerkar,

    Thank you for your interest in OpenSesame!

    No, there's no way to do that through the GUI, but you can set the duration to '0' and add a simple inline_script after the sketchpad:

    # Import and create mouse and keyboard objects
    from openexp.keyboard import keyboard
    from openexp.mouse import mouse
    my_keyboard = keyboard(self.experiment, timeout=5)
    my_mouse = mouse(self.experiment, timeout=5)
    
    # Wait until a key has been pressed or button has been clicked
    start_time = self.time()
    while True:
        resp, time = my_keyboard.get_key()
        if resp != None:
            resp = my_keyboard.to_chr(resp)
            break       
        resp, pos, time = my_mouse.get_click()
        if resp != None:
            break
            
    # Store the response
    self.experiment.set("response", resp) 
    self.experiment.set("response_time", time-start_time)

    For more information, see

    Hope this helps!

    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

  • edited 7:01AM

    Thanks, that helps a lot.

    Ezer

  • Very helpful answer. Thanks too.

    One more detail:
    If I only want certain keypresses, lets say F, G,H, and J, plus any mouse clicks, where do i insert this?

    like that?
    resp, time = my_keyboard.get_key(f;g;h;j)

  • edited September 2016

    Hi,

    The general procedure is explain here and here

    If you want to use both, mouse and keyboard responses, you should sample responses in a loop, e.g. like so:

    <br />kb = keyboard(keylist=['j', 'f','g', 'h'], timeout=20)
    # have a low timeout, so that the experiment will not freeze in anticipation for a keyboard response)
    ms = mouse(timeout = 20)
    
    t0 = clock.time()
    while clock.time()-t0 < 5000: # have 5 seconds as response time limit to avoid infinite loops and such
        key, time = kb.get_key()
        if key != None:
            break
        button, position, timestamp = my_mouse.get_click()
        if button is not None:
            break
    

    I hope this helps.

    Eduard

Sign In or Register to comment.