根据PsychoPy中的用户输入停止试用推进

时间:2016-12-20 20:38:48

标签: python psychopy

我正在使用PsychoPy编写EyeTracker机器的实验。

正如PsychoPy谷歌小组所建议的,我使用了构建器视图来创建实验的基本结构,编译脚本然后在编码器视图上添加了修改,因此处理用户输入的方式是由PsychoPy。由于我需要用户写下他/她的输入,然后按Enter键前进到下一个问题,我有2个键响应对象,所以第一个收集用户的输入,而第二个按Enter键时触发。例如:

        # *B1_P2_key_01* updates
        if t >= 0.0 and B1_P2_key_01.status == NOT_STARTED:
            # keep track of start time/frame for later
            B1_P2_key_01.tStart = t  # underestimates by a little under one frame
            B1_P2_key_01.frameNStart = frameN  # exact frame index
            B1_P2_key_01.status = STARTED
            # keyboard checking is just starting
            win.callOnFlip(B1_P2_key_01.clock.reset)  # t=0 on next screen flip
        if B1_P2_key_01.status == STARTED:
            #theseKeys = event.getKeys(keyList=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'])
            theseKeys = event.getKeys(keyList=['num_1', 'num_2', 'num_3', 'num_4', 'num_5', 'num_6', 'num_7', 'num_8', 'num_9', 'num_0'])
            # check for quit:
            if "escape" in theseKeys:
                endExpNow = True
            if len(theseKeys) > 0 and b1p2_counter < 3:  # at least one key was pressed
                B1_P2_key_01.keys.extend(theseKeys)  # storing all keys
                B1_P2_key_01.rt.append(B1_P2_key_01.clock.getTime())
                print "accepted key"
                b1p2_counter += 1

        # *B1_P2_key_02* updates
        if t >= 0.0 and B1_P2_key_02.status == NOT_STARTED:
            # keep track of start time/frame for later
            B1_P2_key_02.tStart = t  # underestimates by a little under one frame
            B1_P2_key_02.frameNStart = frameN  # exact frame index
            B1_P2_key_02.status = STARTED
            # keyboard checking is just starting
            win.callOnFlip(B1_P2_key_02.clock.reset)  # t=0 on next screen flip
            event.clearEvents(eventType='keyboard')
        if B1_P2_key_02.status == STARTED:
            theseKeys = event.getKeys(keyList=['return'])

            # check for quit:
            if "escape" in theseKeys:
                endExpNow = True
            if len(theseKeys) > 0:  # at least one key was pressed
                B1_P2_key_02.keys = theseKeys[-1]  # just the last key pressed
                B1_P2_key_02.rt = B1_P2_key_02.clock.getTime()
                # a response ends the routine
                msg2='finish_b1p2_'+paths_list[imagecounter]
                print msg2
                #tracker.sendMessage(msg2)
                continueRoutine = False

请注意,我需要用户的输入在他/她输入时显示在屏幕上,所以我已经实现了一个基于使用另一个TextStim对象的解决方案,如同组中所建议的那样:

        # *B1_P2_resp_01* updates
        if t >= 0.0 and B1_P2_resp_01.status == NOT_STARTED:
            # keep track of start time/frame for later
            B1_P2_resp_01.tStart = t  # underestimates by a little under one frame
            B1_P2_resp_01.frameNStart = frameN  # exact frame index
            B1_P2_resp_01.setAutoDraw(True)

        displaystring=" ".join(B1_P2_key_01.keys) #convert list of pressed keys to string
        displaystring=displaystring.replace(' ','') #remove intermediate spaces
        # Do some text cleanup...replacing key names with puntuation and effectively disabling keys like 'back','shift', etc.
        displaystring=displaystring.replace('space',' ') 
        displaystring=displaystring.replace('comma',',')
        displaystring=displaystring.replace('lshift','')
        displaystring=displaystring.replace('rshift','')
        displaystring=displaystring.replace('period','.')
        displaystring=displaystring.replace('back','')
        displaystring=displaystring.replace('num_1','1')
        displaystring=displaystring.replace('num_2','2')
        displaystring=displaystring.replace('num_3','3')
        displaystring=displaystring.replace('num_4','4')
        displaystring=displaystring.replace('num_5','5')
        displaystring=displaystring.replace('num_6','6')
        displaystring=displaystring.replace('num_7','7')
        displaystring=displaystring.replace('num_8','8')
        displaystring=displaystring.replace('num_9','9')
        displaystring=displaystring.replace('num_0','0')
        #displaystring=displaystring.replace('backspace','')
        #set text of AnswerDisplay to modified string
        B1_P2_resp_01.setText(displaystring)
        #print displaystring

现在我的问题是如果用户没有输入任何内容,或者提供的值超出给定范围(例如:值只能从0到100),如何阻止用户前进。我知道有一些替代方案,但使用的结构与PsychoPy的默认结构不同。我需要在当前提供的输入控件上实现此输入控件。

我试图处理Enter事件,但文本对象仍记录按下的回车键,因此它会更改其文本。我也尝试从第一个key_response对象控制它,但是另一个负责处理Enter键的人停止工作。

长话短说,我需要为第二个key_response对象(处理Enter键的对象)设置一个条件,以决定用户是否可以进入下一个试用版,同时坚持这种格式,再加上不影响on屏幕显示输入。

非常感谢有关此主题的帮助。

1 个答案:

答案 0 :(得分:1)

您还没有包含关键部分,这是您第一个代码段的循环。这个循环可能就像

while continueRoutine:
      # Your code here

...运行代码,直到continueRoutine设置为False,这在用户按下return时发生。您想再添加一个条件。所以像这样:

displaystring = ''  # to prevent carry-over from the last trial in the first iteration of the loop.
continueRoutine = True  # this is probably already set higher up in the script and could be ignored
while continueRoutine and displaystring:
    # your code here

您可以非常灵活地使用这些条件,例如:如果你想要通过至少5秒,你会做

while continueRoutine and displaystring and t < 5: