def getKeys(): keys = [] while PsychHID('KbQueueFlush'): evt = PsychHID('KbQueueGetEvent')[0] if evt['Pressed']: K = chr(int(evt['CookedKey'])).lower() keys.append((K, evt['Time'])) return keys
def kb_init(): # create the keyboard queue PsychHID("KbQueueCreate") # Psychtoolbox # start logging keyboard button presses PsychHID("KbQueueStart") # initializations key_new = False key_value = [] read_last = time.time() read_interval = 1 / 3.1 # every 1/3.1 sec return key_new, key_value, read_last, read_interval
def kb_read(): [key_new, firstPress, _, _, _] = PsychHID("KbQueueCheck") # time when keyboard was last checked read_last = time.time() # if there was a key press find the key name if key_new: ind1, ind2 = np.unravel_index(firstPress.argmax(), firstPress.shape) if (ind2) == 26: key_value = "ESCAPE" elif (ind2) == 12: key_value = "Return" else: key_value = chr(ind2 + 1) else: key_value = [] return read_last, key_new, key_value
def _replacement_create_queue(self, num_slots=10000, flags=0, win_handle=None): PsychHID('KbQueueCreate', self.device_number, None, 0, num_slots, flags, win_handle)
instr_txt = ( "In this experiment,\npress the left key (←) when you see a sleeping cat,\n" + "and the right key (→) when you see an awake cat.\n" + "Respond as quickly as possible!\n\nPress any key to start.") instructions = visual.TextStim(win, instr_txt, height=0.2 / 5) good_txt = visual.TextStim(win, 'GOOD', height=0.1, color='green') bad_txt = visual.TextStim(win, 'BAD', height=0.1, color='red') # input setup key_list = np.zeros(256) key_list[36] = 1 # left key key_list[38] = 1 # right key PsychHID('KbQueueCreate', 0, key_list) # trial order num_trials = 10 indices = [randint(0, len(imgs) - 1) for i in range(num_trials)] block_data = [ dict.fromkeys( ['awake', 'name', 'start_time', 'rt', 'choice', 'correct']) for i in range(num_trials) ] instructions.draw() win.flip() event.waitKeys() core.wait(2)
def kb_close(): PsychHID("KbQueueRelease") return
def close_backend(): if KB_BACKEND == 'psychhid': PsychHID('KbQueueStop') elif io: io.quit()
def getKeys(): keys = io.devices.keyboard.getPresses() return [(k.key, k.time) for k in keys] elif KB_BACKEND == 'psychopy.keyboard.iohub': from psychopy.iohub import launchHubServer io = launchHubServer() from psychopy.hardware.keyboard import Keyboard as ptbKeyboard ptb_kb = ptbKeyboard() def getKeys(): keys = ptb_kb.getKeys(waitRelease=psychopy_keyboard_waitRelease) return [(k.name, k.tDown) for k in keys] elif KB_BACKEND == 'psychhid': from psychtoolbox import PsychHID ptb_keys = [1] * 256 PsychHID('KbQueueCreate', [], ptb_keys) PsychHID('KbQueueStart') def getKeys(): keys = [] while PsychHID('KbQueueFlush'): evt = PsychHID('KbQueueGetEvent')[0] if evt['Pressed']: K = chr(int(evt['CookedKey'])).lower() keys.append((K, evt['Time'])) return keys def close_backend(): if KB_BACKEND == 'psychhid': PsychHID('KbQueueStop') elif io: