psychopy.org | Reference | Downloads | Github

Pyxid function similar to thisKey in []:

Hello,

I have recently got a response box to work with pyxid. However there is very limited information on the functions for pyxid and how to code using it. Before I was using event.getKeys() and it would listen for a key press and not do anything if nothing was pressed. Now using the 2nd bit of code I have written out, it now waits for a button to be pressed and doesn’t move on through the experiment and once a button is pressed it exits the experiment. I think this could be because of the while True part, but I’m really not sure. Any help or links to materials or examples is appreciated!

previously my code was like this and worked:

for l in range(0,(int(numFrames*1.5))): 
    myFix.draw()
    myWin.flip
    allKeys=event.getKeys()
        for thisKey in allKeys:
        if thisKey in ['space']:
            buttonTime = time.time()
            buttonPressed = 1
            print 'space'
            respTime = buttonTime - stimTime
            print 'respTime is:'
            print respTime
            this_response = (respTime, cat_repeat)
            responses.append(this_response)
            totalResponse += respTime
            rowCounter += 1
            if cat_repeat == "correct":
                correct += 1
            with open(info['Participant'], 'ab') as csvfile:
                 wr = csv.writer(csvfile, delimiter=',')
                 wr.writerow(this_response)
                 print "cat_repeat"
                 print cat_repeat
             if thisKey in ['q', 'escape']:
                 core.quit()

Now I have changed my code to:

for l in range(0,(int(numFrames*1.5))):
    myFix.draw()
    myWin.flip()
    if myBox.is_response_device():
        while True:
            myBox.poll_for_response()
            if myBox.response_queue_size()>0:
                boxResponse = myBox.get_next_response()
                buttonTime = time.time()
                buttonPressed = 1
                print "box pressed"
                respTime = buttonTime - stimTime
                print "respTime is:"
                print respTime
                this_response = (respTime, cat_repeat)
                responses.append(this_response)
                totalResponse += respTime
                rowCounter += 1
                if cat_repeat == "correct":
                    correct += 1
                with open(info['Participant'], 'ab') as csvfile:
                     wr = csv.writer(csvfile, delimiter=',')
                     wr.writerow(this_response)
                     print "cat_repeat"
                     print cat_repeat
                 if thisKey in ['q', 'escape']:
                     core.quit()

Hi, you’re correct, the while loop is infinite.

You can exit out of the while loop with a break statement after you’ve done the data logging etc.
The premature quitting of the experiment is probably caused by the last if statement; thisKey isn’t defined in the second example so it’s probably using a value from earlier in your script to evaluate to True.

When I did some XID stuff a few months ago I rephrased it as a function to allow easy re-use, something like:

def checkXID(myBox):
	myBox.poll_for_response()
	if myBox.response_queue_size() > 0:
		resp = myBox.get_next_response()
		if resp['pressed']:
			return True
		else:
			return False

This just checks to see if there was any pressed responses, for use with a Cedrus voice key, but can be adapted to return the value of the pressed button.

Thank you very much jgoodliffe. This works perfectly.

m