Howdy, Stranger!

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

Supported by

Coding a loop

Hi Everybody,

I have tried different ways to do the following using the usual commands of OS but it looks tedious and tricky because this small routine does not fit the original scope of OpenSesame.
I guess it would be more effective to create an in-line script. Unfortunaltely, I don't master Python. Can you help me ? This is the structure of the routine in "natural" language:

do :

  • display "black screen"
  • wait until a keypress (y or n or esc)
  • if key = y, display "YES" (during a set time)
  • if key = n, display "NO" (during a set time)
    loop if key <> esc

"black screen", "YES" and "NO" are scketchpads.

To sum up, this routine allows a continuous monitoring of the Y/N choices of the user.
Nothing else ...

Thank you in advance !

Comments

  • edited February 14

    Hi,

    To learn python, check out some resources on our website. Or anywhere on the web, really.... It is totally worth the time investment. The future you will be very grateful.

    In theory, what you describe can also be done with sketchpad`s. You just have to make sure that you have enough repeats in your sequence and make use of the run-if statement of the items in the sequence.

    However, you are right in the sense that using Python, setting this up is a little more straightforward.

    here a little example. It is not tested, so it might not work out of the box.

    cv1 = Canvas()
    cv2 = Canvas()
    cv1.text('yes')
    cv2.text('no')
    
    kb = Keyboard()
    while True: 
         k,t = kb.get_key()
         if k!=None:
             if k=='y':
                  cv1.show()
             elif k=='n':
                  cv2.show()
             elif k == 'q':
                  break
    

    Good luck,

    Eduard

  • Hi Eduard,

    Thank you very much for your help !
    Your routine works fine. I just have to adapt it to the context of the experiment.

    Best regards,
    Gérard

  • Hi Eduard,

    I have taken your advise and built a similar sequence using sketchpads and it works fine with keyboard entries. But I cannot work it out with mouse clics. The only differences in the sequence are the entry device and of course the corresponding conditional statements of the sequence. It looks like the program operates in the mouse entry step (clicks are visible) but that it does not continue in the sequence after a click. It continues to accept other clicks. It is difficult for me to provide more information because I am not allowed to upload screen captures. I have encountered the same problem when I tried to build other sequences. The mouse entry block does not work. Any advise or questions ?

    Thank you in advance!
    Gérard

  • Hi Gérard,

    Mouse items work a little differently than Keyboard items. If you check out the documentation, you will see that the mouse item returns three things: which button was clicked (left, right), the position of the click (x,y) and the timestamp of the click. A keyboard item on the other hand, returns only two things, the key and the timestamp. So the script that I wrote catches both key and timestamp (in the variables k and t) and breaks the loop once a key has been pressed.

    However, unless you have three mouse buttons you can't use the same logic, because you need a third button to exit the loop. To solve it, you have 2 options (that I can think of). First, you can define buttons on the screen, that do certain things (check out the mouse trap plugin). Second, you can add some complex logic to the loop. For example the script below, will interpret 2 right clicks in a row as the break-the-loop-signal.

    state = 'none'
    while True: 
         button,position,time = mouse.get_click()
         if button!=None:
             if button=='left':
                  cv1.show()
                  state = 'left'
             elif button=='right':
                  cv2.show()
                  if state == 'right':
                      break
                  state = 'right'
    

    Does this make sense?

    Eduard

  • Hi Eduard,

    Thank you for this interesting information based on using an inline_script. But I am stuck at a lower level because I am not able to work out this very simple sequence that I have to describe in detail for your understanding (uploads of screenshots are prohibited).

    The structure of that test program is:
    Main experiment -> Main sequence -> Loop -> 'Display' sequence.

    Parameters of the Loop:
    Execute 'Display', repeat each cycle 1 time, order sequential, source table, exit never, evaluate on 1st cycle, do not restart after the pause. The table is empty.

    The 'Display' sequence contains 4 items:
    'Black screen' sketchpad -> 'Rep' mouse_response item -> 'Yes' sketchpad -> 'No' sketchpad.

    Parameters of the 'Rep' mouse_response item :
    Correct response 'left_button', valid responses 'left_button;right_button, max delay infinite, no sketchpad is linked, event mouseclick, show the cursor, delete previous clicks.

    Conditions assigned to the items of the 'Display' sequence:
    'Black screen' : always; 'Rep' : always; 'Yes' : [response_Rep]= "left_button"; 'No' : [response_Rep] = "right_button"

    It does not work ... Nothing happens when you click. The 'Yes' and 'No' sketchpads do not show up.

    But if I replace the mouse_response item with a keyboard_response item and that I adapt the conditions attached to the 'Display' sequence according to the keys used to activate 'Yes' and 'No', it works.

    So what ?!

    Thank you very much for your time,
    Gérard

  • Hi Eduard,

    I discovered that the cause of the problem is that the mouse button names (left_button, right_button ...) that are referenced in the OpenSesame documentation DO NOT WORK. Instead, it is necessary to use the button numbers 1, 2, 3 ... The problem is solved. Thank you again for your help.

    Best,
    Gérard

Sign In or Register to comment.