I have found that the ioHub getPresses() command works in the absence of a window and this is a good alternative to event.getKeys(). I initially struggled to understand how to clear the keyboard buffer prior to the getPresses() statement, because clearEvents() did not work; but then I discovered that clearEvents(device_label=‘keyboard’) does the trick. (In the code below, the first getPresses() command has clear=False so that it leaves a keypress event in the buffer for clearEvents to clear up.)
from psychopy.iohub import launchHubServer
io=launchHubServer()
keyboard = io.devices.keyboard
print "START"
while True:
if keyboard.getPresses(clear=False):
print "event"
break
io.clearEvents(device_label='keyboard')
print "MIDDLE"
while True:
if keyboard.getPresses():
print "event"
break
print "END"