Howdy, Stranger!

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

Supported by

[open] getting keyboard_response to accept double digit responses

edited December 2013 in OpenSesame

as the title says, i'm trying to get the keyboard_response function to accept 2 digits rather than skipping forward after a single key press. Probably not a complex question... any advice?

Comments

  • edited December 2013

    This is generally not something you can do with a keyboard response (unless you put two of them directly after eachother, but I have the feeling this is not what you would want). You can do two things:

    1) Use the form_text_input plugin, which allows users to type their response in.

    2) Use a bit of inline scripting, like the snippet below (replace your keyboard_response with an inline_script and put the code in its Run Phase to test it).

    # first, we import the classes that we need for our script
    from openexp.canvas import canvas
    from openexp.keyboard import keyboard
    
    # then, we use these classes to create objects that we
    # are going to use in our script: first the canvas object,
    # which we will use to display stuff on the screen
    my_canvas = canvas(exp)
    # second the keyboard object, which we will use to
    # register keypresses
    my_keyboard = keyboard(exp)
    
    # now, we will need to pre-define some stuff; in this case
    # we will need the allowed numbers:
    numbers = ['0','1','2','3','4','5','6','7','8','9']
    # and an empty string, in which we are going to store the
    # response that was given by the participant
    numstring = ''
    # also, we will need to define the maximal amount of
    # numbers that can be put in
    maxlength = 2
    
    # now, before we start, let's clear the display and get the
    # starting time (useful for calculating the RT later on!)
    my_canvas.clear()
    t0 = my_canvas.show()
    
    # now, we are going to make sure the display stays on
    # until the participant presses the space button; we will
    # do so using a while loop, which runs until the 'sentry
    # variable' (in this case it's 'keeprunning') will go False
    keeprunning = True
    while keeprunning:
    
        # check if there is any keyboard input
        key, presstime = my_keyboard.get_key()
    
        # check if the input is a number
        if key in numbers:
            # then check if the number string we have is not
            # too long
            if len(numstring) < maxlength:
                # then add the number to the string!
                numstring += key
    
        # if the input was not a string, check if it was backspace
        elif key == 'backspace':
            # if it was backspace, remove the last number from
            # the string that contains the response
            numstring.pop(-1)
    
        # finally, if the input was not a string and not a number,
        # check if it was space
        elif key == 'space':
            # if it was, we can stop the loop
            keeprunning = False
    
        # now that we have the input, we need to show it to
        # the participant; first we clear the canvas, so the
        # previous contents is removed
        my_canvas.clear()
        # then we draw the current number
        my_canvas.text(numstring)
        # and we show the canvas, updating the display
        my_canvas.show()
    
    # after running the loop, set some variables to store the
    # response and the response time
    exp.set("response", numstring)
    exp.set("response_time", resptime - t0)
    
Sign In or Register to comment.