2016-12-05 19 views
0

在下面的代碼中,我顯示pic1 3秒,然後顯示1秒的固定十字,然後我顯示pic2,直到用戶按下關鍵。據我所知,它應該只是在我的3rd'for'循環中收集按鍵,因爲這是我創建按鍵和檢查按鍵等的地方。但是,如果我在按鍵1或按鍵過程中按下按鍵,一旦pic2出現,它會立即繼續執行代碼。它似乎是在我的第三個'for循環'之前註冊按鍵,然後在我的第三個for循環開始時發揮作用。我不明白這是如何發生的,因爲我在顯示pic1和注視過程中沒有檢查任何按鍵。有人能在這裏啓發我嗎?也許我誤解了getKeys的一些基本問題。Python/Psychopy:getKeys過早收集密鑰

- 如果我沒有按任何東西,那麼預期的行爲發生..它顯示圖片2,並等待一個關鍵。如果按下按鍵,它只會隨代碼一起移動,或者如果過了60秒(我已將圖像設置爲顯示60秒,用戶需要在前5秒內響應,所以60就是安全)。

def block1(): 

    running = 1 

    while running ==1: 

     for frames in range(image_frames): #3 seconds 
      pic1[0].draw() 
      window.flip() 

     for frame in range(fixation):  #1 second 
      fix.draw() 
      window.flip() 

     for frames in range(stim_Frame): #only moves on with keypress (or 60 secs) 
      pic2[0].draw() 
      start = window.flip() 
      if frames == 0: 
       stim_time = start 
       print "stim time: " 
       print stim_time 

      allKeys = event.getKeys(keyList = ('f','h','escape')) 
      for thisKey in allKeys: 
       if thisKey == 'escape': 
        print "you quit" 
        window.close() 
        core.quit() 
       if thisKey == 'f': 
        keyTime=core.getTime() 
        thisResp = 1  
        print "keytime is: " 
        print keyTime 

       elif thisKey == 'h': 
        keyTime=core.getTime() 
        thisResp = 0 
        print "keytime is: " 
        print keyTime 

      if thisResp == 1 or thisResp == 0: 
       break 

     running = 2 

    window.flip() 

乾杯, 史蒂夫

+2

我的理解是'event.getKeys()'會返回其內存緩衝區中的所有鍵。嘗試在使用['event.clearEvents()'](http://www.psychopy.org/api/event.html#psychopy.event.clearEvents) – Jakub

+0

Yup工作的第三個循環之前清除該緩衝區,謝謝。我不知道它會返回緩衝區中的所有鍵,甚至在啓動getKeys之前按下鍵。 – Steve

+0

@Jakub是正確的。將它添加爲答案而不是評論並且接受它會很好。如果沒有,你可以回答你自己的問題,史蒂夫。 –

回答

1

event.getKeys()返回所有鍵存在於它的存儲緩衝器中。在第三個循環之前清除緩衝區。

def block1(): 

    running = 1 

    while running ==1: 

     for frames in range(image_frames): #3 seconds 
      pic1[0].draw() 
      window.flip() 

     for frame in range(fixation):  #1 second 
      fix.draw() 
      window.flip() 

     event.clearEvents() # Clear the previously pressed keys. 

     for frames in range(stim_Frame): #only moves on with keypress (or 60 secs) 
      pic2[0].draw() 
      start = window.flip() 
      if frames == 0: 
       stim_time = start 
       print "stim time: " 
       print stim_time 

      allKeys = event.getKeys(keyList = ('f','h','escape')) 
      for thisKey in allKeys: 
       if thisKey == 'escape': 
        print "you quit" 
        window.close() 
        core.quit() 
       if thisKey == 'f': 
        keyTime=core.getTime() 
        thisResp = 1  
        print "keytime is: " 
        print keyTime 

       elif thisKey == 'h': 
        keyTime=core.getTime() 
        thisResp = 0 
        print "keytime is: " 
        print keyTime 

      if thisResp == 1 or thisResp == 0: 
       break 

     running = 2 

    window.flip()