Collecting responses

Objectives

  • Be able to collect responses from the keyboard, knowing which key was pressed and when it was pressed.

We have already encountered the way in which we can collect participant responses via our use of psychopy.event.waitKeys() and psychopy.event.getKeys(). Here, we will take a closer look at these functions and how we can use them in vision science experiments.

Knowing which keys were pressed

We often want to know which key was pressed, not just that any of the keys were pressed. The “key” functions in psychopy return a list of strings, where each item in the list is a string representation of the key that was pressed. For example, if we run the following and press the “d” key, we can see that the item in the list is “d”:

import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
    size=[400, 400],
    units="pix",
    fullscr=False
)

keys = psychopy.event.waitKeys()

print keys

win.close()
['d']

Tip

Why is keys a list, rather than just a string? This is to account for a situation in which multiple keys are pressed simultaneously.

The string that represents the key that was pressed is straightforward for most of the keys. However, we often want to use the “arrow” keys, and it is not immediately obvious what the string representation will be. Let’s have a look by running the above again but pressing the “left arrow” key:

import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
    size=[400, 400],
    units="pix",
    fullscr=False
)

keys = psychopy.event.waitKeys()

print keys

win.close()
['left']

As you can see, the arrow keys correspond to “left”, “right”, “up”, and “down”. If you are unsure what the string representation of a particular key is, you can use a method like that shown above to find out.

Restricting the available keys

During an experiment, a participant usually only has a narrow range of keys that are meaningful. To prevent an accidental keypress prematurely ending a call to waitKeys, we can provide a keyList argument that restricts the keys that it “listens” to. For example, if we know that participants can only respond by pressing the left or right arrow key, it is often sensible to use:

import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
    size=[400, 400],
    units="pix",
    fullscr=False
)


keys = psychopy.event.waitKeys(keyList=["left", "right"])

win.close()

This way, the only way that waitKeys will return is if the left or right arrow key is pressed—it will keep waiting if another key, such as the spacebar, is pressed.

Determining when a key was pressed

It can also be useful know the time that a keypress was made (such as in studies of reaction time, though care needs to be taken if precise time estimates are required). We can easily obtain this information by providing a timeStamped argument with a timing clock:

import psychopy.visual
import psychopy.event
import psychopy.core

win = psychopy.visual.Window(
    size=[400, 400],
    units="pix",
    fullscr=False
)

clock = psychopy.core.Clock()

keys = psychopy.event.waitKeys(timeStamped=clock)

print keys

win.close()
[('left', 0.5030858516693115)]

You can see that what is being returned to us now is a list of lists (technically a list of tuples, which are similar to lists), where each item in the list is a pairing of the key that was pressed and the time it was pressed, relative to the clock timing that was provided to the function.