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: 2024-07-22 22_14_45.456732
file: /home/circleci/project/examples/experiments/keyrelease.py
participant: foo
session: 001
2024-07-22 22:14:45,456 - INFO    - Expyfun: Using version 2.0.0.dev0 (requested dev)
2024-07-22 22:14:45,457 - INFO    - Expyfun: Setting up sound card using pyglet backend with 2 playback channels
2024-07-22 22:14:45,632 - 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.
2024-07-22 22:14:45,633 - INFO    - Expyfun: Setting up screen
2024-07-22 22:14:45,662 - EXP     - Expyfun: Set screen visibility True
2024-07-22 22:14:45,674 - INFO    - Initialized [1280  960] window on screen XlibScreen(display=<pyglet.canvas.xlib.XlibDisplay object at 0x7f019f79fd90>, x=0, y=0, width=1400, height=900, xinerama=0) with DPI 69.73
2024-07-22 22:14:45,675 - INFO    - Expyfun: Initializing dummy triggering mode
2024-07-22 22:14:45,676 - INFO    - Expyfun: Initialization complete
2024-07-22 22:14:45,676 - EXP     - Expyfun: Participant: foo
2024-07-22 22:14:45,676 - EXP     - Expyfun: Session: 001
2024-07-22 22:14:46,754 - INFO    - Expyfun: Exiting
2024-07-22 22:14:46,756 - EXP     - Expyfun: Audio stopped and reset.

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

from expyfun import ExperimentController, building_doc
from expyfun import analyze as ea

print(__doc__)

isi = 0.5
wait_dur = 3.0 if not building_doc else 0.0
msg_dur = 3.0 if not building_doc else 0.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 = [f"{k} {r} after {round(t, 4)} secs\n" "" 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.378 seconds)

Gallery generated by Sphinx-Gallery