Howdy, Stranger!

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

Supported by

[solved] How to have text input with an image (and updating)

edited October 2014 in OpenSesame

Hello,

This may be redundant, but the last thread I could find on this question was a few years old. So, I'll ask it again for the newer version of opensesame.

I need my participants to be able to view a picture on sketchpad and then enter a multi-character keyboard response. Right now I have them viewing the sketchpad, doing a keypress to advance, and then using text_input to enter their response.

Alternatively, a more complicated arrangement that I would like is for participants to be able to order a sequence and receive updates regarding the order (e.g., water jug problem or tower of hanoi problem) and be allowed to advance once they have solved the multi step problem. How might that be possible?

Thank you for your time.
Jared

Comments

  • edited October 2014

    Hi Jared, and welcome to the forum!

    In general, both of your questions can be addressed by inline scripting. However, the first one (typing and concurrently displaying an image) is considerably easier to do that the second one (touch/click interaction with an animated display). The code for the first I give you below, the code for the second is something you could try to go for yourself after familiarizing yourself with Python (see here for some tips).

    Display image and typed text

    Place this code in the Run phase of an inline_script item. Based on my code in this thread. This assumes you have defined a variable imgname in the loop item through which you run this!

    from openexp.canvas import canvas
    from openexp.keyboard import keyboard
    
    # get the image name and path
    fname = exp.get_file(self.get('imgname'))
    
    # vertical coordinate of image and text
    imgy = int(self.get('height') * 0.3)
    txty = int(self.get('height') * 0.8)
    
    # allowed characters
    allowed = 'abcdefghijklmnopqrstuvwxyz.,'
    
    # create objects
    my_kb = keyboard(exp)
    my_canvas = canvas(exp)
    
    # draw the first canvas
    my_canvas.clear()
    my_canvas.image(fname, y=imgy, x=None, scale=None, center=True)
    my_canvas.show()
    
    # run until the Enter key is pressed
    stop = False
    my_string = ""
    while not stop:
        # get keypress
        key, t1 = my_kb.get_key(timeout=None)
        # handle input
        if key.lower() in allowed:
            my_string += key
        elif key == 'space':
            my_string += " "
        elif key in ['enter','return']:
            stop = True
        # update display
        my_canvas.clear()
        my_canvas.image(fname, y=imgy, x=None, scale=None, center=True)
        my_canvas.text(my_string, center=True, color=None, max_width=None, html=True, bidi=None, y=txty, x=None)
        my_canvas.show()
    
    # save the string
    exp.set("response", my_string)
    

    Good luck!

    Edwin

  • edited 4:29AM

    Thanks Edwin!

  • edited 4:29AM

    No worries! Will mark this as solved.

Sign In or Register to comment.