Failure to getKeys

My task is to design a square block. It will run randomly from one side of the screen to the other. And a beep will be played sometime. One has to push the botton(Here is keyboard) as soon as he hears the beep. The question is, when I use the “getkeys” method, it always returns an empty list. The RT shows that it main get the very first input in the buffer. I also tried eventclear, but seemed useless. Again, I tried Keyboard class, and the return remains empty list.

Here is the code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from psychopy import visual, core, event,gui
from psychopy import sound as sd
import pygame
from psychopy.hardware import keyboard
import random, math,time
from psychopy.iohub import launchHubServer
import numpy as np

win = visual.Window(fullscr = False, size = [960,540], color = 'black', units = 'pix')

kb = keyboard.Keyboard()

io = launchHubServer()

kbd = io.devices.keyboard

x,y = win.size

ran = [random.randint(-400,400),random.randint(-400,400),random.randint(-150,150),random.randint(-150,150)]

mode = random.randint(0,3)

start_pos = [[ran[mode],-269],[ran[mode],269],[-479,ran[mode]],[479,ran[mode]]]

rect = visual.Rect(win, width = 50, height = 50, fillColor='white', pos = start_pos[mode], name = 'target')

timer = core.Clock()

dx = [0,0,1,-1]
dy = [1,-1,0,0]

def present(rect,t):
    timer.reset()
    while timer.getTime() < t:
        rect.draw()
        win.flip()


##winSur = pygame.display.set_mode((x, y))
    
def movement(rect,time):
    event.clearEvents()
    x,y = win.size
    timer.reset()
    timer_2 = core.Clock()
    timer_2.reset()
    jump = True
    inp = False
    while timer.getTime() < time and (abs(rect.pos[0])< x/2 and abs(rect.pos[1])< y/2):
        timeUse = timer_2.getTime()
        timer_2.reset()
        a,b = rect.pos
        rect.pos += [dx[mode]*timeUse*100,dy[mode]*timeUse*100]

        if int(timer.getTime()) == 3 :
            if jump :
                sd.Sound('D', octave = 5).play()
            #print("D" , timer.getTime())
            if jump :
                kb.clock.reset()
                kb.start()
            print('++++++++++')
           # event.clearEvents()
            
            if timer.getTime() < time and jump:
                keys = kb.getKeys()
                print('----------')
                print(keys)
                
                
            jump = False
        #print([timer.getTime(),time])
        #print(rect.pos)
        rect.draw()
        win.flip()
        
    




present(rect,1)
movement(rect,15)

I would feel my very gratitude to who helps.

Seems solved. I deleted jump from

if timer.getTime() < time and jump:

Since you are using psychopy.hardware.keyboard, you do not need to use iohub, and probably should not since you will have 3 processes running, two just for keyboard timestamping. So remove:

from psychopy.iohub import launchHubServer

# and

io = launchHubServer()
kbd = io.devices.keyboard

Thank you sol,
but actually here I set launchHubServer just to test another alternative to catch keyboard press. It has no specific effect on the final result. When I did the previous delete operation, it returned the correct press successfully, regardless of the launchHubServer thing.