KeyPressAndRelease demoΒΆ

This example demonstrates gathering key-releases as well as presses with the ExperimentController class.

Please note that this currently only works for the keyboard, which has inprecise timing.

keyrelease
exp_name: KeyPressAndReleaseDemo
date: 2023-07-25 19_08_22.713021
file: /home/circleci/project/examples/experiments/keyrelease.py
participant: foo
session: 001
2023-07-25 19:08:22,713 - INFO    - Expyfun: Using version 2.0.0.dev0 (requested dev)
2023-07-25 19:08:22,714 - INFO    - Expyfun: Setting up sound card using pyglet backend with 2 playback channels
2023-07-25 19:08:22,912 - WARNING - Expyfun: Mismatch between reported stim sample rate (24414) and device sample rate (44100.0). Experiment Controller will resample for you, but this takes a non-trivial amount of processing time and may compromise your experimental timing and/or cause artifacts.
2023-07-25 19:08:22,912 - INFO    - Expyfun: Setting up screen
2023-07-25 19:08:22,943 - EXP     - Expyfun: Set screen visibility True
2023-07-25 19:08:22,956 - INFO    - Initialized [1280  960] window on screen XlibScreen(display=<pyglet.canvas.xlib.XlibDisplay object at 0x7f1a95b502b0>, x=0, y=0, width=1400, height=900, xinerama=0) with DPI 69.73
2023-07-25 19:08:22,956 - INFO    - Expyfun: Initializing dummy triggering mode
2023-07-25 19:08:22,957 - INFO    - Expyfun: Initialization complete
2023-07-25 19:08:22,957 - EXP     - Expyfun: Participant: foo
2023-07-25 19:08:22,957 - EXP     - Expyfun: Session: 001
2023-07-25 19:08:24,058 - INFO    - Expyfun: Exiting
2023-07-25 19:08:24,062 - EXP     - Expyfun: Audio stopped and reset.

# Author: Jasper van den Bosch <jasperb@uw.edu>
#
# License: BSD (3-clause)

from expyfun import ExperimentController, building_doc, analyze as ea

print(__doc__)

isi = 0.5
wait_dur = 3.0 if not building_doc else 0.
msg_dur = 3.0 if not building_doc else 0.

with ExperimentController('KeyPressAndReleaseDemo', screen_num=0,
                          window_size=[1280, 960], full_screen=False,
                          stim_db=0, noise_db=0, output_dir=None,
                          participant='foo', session='001',
                          version='dev', response_device='keyboard') as ec:
    ec.wait_secs(isi)

    ###########################################
    # listen_presses / while loop / get_presses(kind='both')
    instruction = ("Press and release some keys\n\nlisten_presses()"
                   "\nwhile loop {}\n"
                   "get_presses(kind='both', return_kinds=True)")
    disp_time = wait_dur
    countdown = ec.current_time + disp_time
    ec.call_on_next_flip(ec.listen_presses)
    ec.screen_text(instruction.format(disp_time))
    screenshot = ec.screenshot()
    ec.flip()
    while ec.current_time < countdown:
        cur_time = round(countdown - ec.current_time, 1)
        if cur_time != disp_time:
            disp_time = cur_time
            # redraw text with updated disp_time
            ec.screen_text(instruction.format(disp_time))
            ec.flip()
    events = ec.get_presses(kind='both', return_kinds=True)
    ec.write_data_line('listen / while / get_presses', events)
    if not len(events):
        message = 'no keys pressed'
    else:
        message = ['{} {} after {} secs\n'
                   ''.format(k, r, round(t, 4)) for k, t, r in events]
        message = ''.join(message)
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

ea.plot_screen(screenshot)

Total running time of the script: ( 0 minutes 1.438 seconds)

Gallery generated by Sphinx-Gallery