Skip to main content
Skip table of contents

How to read button presses from a RESPONSEPixx

This demo shows how to read and report button presses using the RESPONSEPixx and the digital input log.

from pypixxlib.datapixx import DATAPixx3
from psychopy import core

#connect to VPixx device
device = DATAPixx3()

#First, let's make a dictionary of codes that correspond to our buttons. This is in decimal.
#Note 1: these codes ARE NOT UNIVERSAL. You can check what your own button codes are using the PyPixx Digital I/O demo
#Note 2: these codes are for single button presses only. If two buttons are pressed at the same time this will generate a unique code, which we will ignore
buttonCodes = {65527:'blue', 65533:'yellow', 65534:'red', 65531:'green', 65519:'white', 65535:'button release'}
exitButton = 'white'

myLog = device.din.setDinLog(12e6, 1000)
startTime = device.getTime()
finished = False

#let's create a loop which checks the schedule at 0.25 s intervals for button presses.
#Any time a button press is found, we print the timestamp and button pressed.
#If a designated exit button is pressed, we disconnect.
while finished == False:
    #read device status
    newEvents = myLog["newLogFrames"]

    if newEvents > 0:
        eventList = device.din.readDinLog(myLog, newEvents)

        for x in eventList:
           if x[1] in buttonCodes:
                #look up the name of the button
                buttonID = buttonCodes[x[1]]

                #get the time of the press, since we started logging
                time = round(x[0] - startTime, 2)
                printStr = 'Button pressed! Button code: ' + str(x[1]) + ', Button ID: ' + buttonID + ', Time:' + str(time)
                if buttonID == exitButton:
                    finished = True

#Stop logging
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.