Keypress demo#

This example demonstrates the different keypress-gathering techniques available in the ExperimentController class.

keypress
exp_name: KeypressDemo
date: 2024-10-08 13_42_54.369834
file: /home/circleci/project/examples/experiments/keypress.py
participant: foo
session: 001
2024-10-08 13:42:54,370 - INFO    - Expyfun: Using version 2.0.0.dev0 (requested dev)
2024-10-08 13:42:54,371 - INFO    - Expyfun: Setting up sound card using pyglet backend with 2 playback channels
2024-10-08 13:42:54,589 - 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-10-08 13:42:54,590 - INFO    - Expyfun: Setting up screen
2024-10-08 13:42:54,623 - EXP     - Expyfun: Set screen visibility True
2024-10-08 13:42:54,631 - INFO    - Initialized [640 480] window on screen XlibScreen(display=<pyglet.canvas.xlib.XlibDisplay object at 0x7f392c363f10>, x=0, y=0, width=1400, height=900, xinerama=0) with DPI 69.73
2024-10-08 13:42:54,631 - INFO    - Expyfun: Initializing dummy triggering mode
2024-10-08 13:42:54,632 - INFO    - Expyfun: Initialization complete
2024-10-08 13:42:54,632 - EXP     - Expyfun: Participant: foo
2024-10-08 13:42:54,632 - EXP     - Expyfun: Session: 001
2024-10-08 13:42:58,216 - INFO    - Expyfun: Exiting
2024-10-08 13:42:58,218 - EXP     - Expyfun: Audio stopped and reset.

# Author: Dan McCloy <drmccloy@uw.edu>
#
# License: BSD (3-clause)

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

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(
    "KeypressDemo",
    screen_num=0,
    window_size=[640, 480],
    full_screen=False,
    stim_db=0,
    noise_db=0,
    output_dir=None,
    participant="foo",
    session="001",
    version="dev",
) as ec:
    ec.wait_secs(isi)

    ###############
    # screen_prompt
    pressed = ec.screen_prompt(
        "press any key\n\nscreen_prompt(" f"max_wait={wait_dur})",
        max_wait=wait_dur,
        timestamp=True,
    )
    ec.write_data_line("screen_prompt", pressed)
    if pressed[0] is None:
        message = "no keys pressed"
    else:
        message = f"{pressed[0]} pressed after {round(pressed[1], 4)} secs"
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

    ##################
    # wait_for_presses
    ec.screen_text(f"press some keys\n\nwait_for_presses(max_wait={wait_dur})" "")
    screenshot = ec.screenshot()
    ec.flip()
    pressed = ec.wait_for_presses(wait_dur)
    ec.write_data_line("wait_for_presses", pressed)
    if not len(pressed):
        message = "no keys pressed"
    else:
        message = [
            f"{key} pressed after {round(time, 4)} secs\n" "" for key, time in pressed
        ]
        message = "".join(message)
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

    ############################################
    # wait_for_presses, relative to master clock
    ec.screen_text(
        f"press some keys\n\nwait_for_presses(max_wait={wait_dur}, " "relative_to=0.0)"
    )
    ec.flip()
    pressed = ec.wait_for_presses(wait_dur, relative_to=0.0)
    ec.write_data_line("wait_for_presses relative_to 0.0", pressed)
    if not len(pressed):
        message = "no keys pressed"
    else:
        message = [
            f"{key} pressed at {round(time, 4)} secs\n" "" for key, time in pressed
        ]
        message = "".join(message)
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

    ##########################################
    # listen_presses / wait_secs / get_presses
    ec.screen_text(
        f"press some keys\n\nlisten_presses()\nwait_secs({wait_dur})" "\nget_presses()"
    )
    ec.flip()
    ec.listen_presses()
    ec.wait_secs(wait_dur)
    pressed = ec.get_presses()  # relative_to=0.0
    ec.write_data_line("listen / wait / get_presses", pressed)
    if not len(pressed):
        message = "no keys pressed"
    else:
        message = [
            f"{key} pressed after {round(time, 4)} secs\n" "" for key, time in pressed
        ]
        message = "".join(message)
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

    ####################################################################
    # listen_presses / wait_secs / get_presses, relative to master clock
    ec.screen_text(
        "press a few keys\n\nlisten_presses()"
        f"\nwait_secs({wait_dur})\nget_presses(relative_to=0.0)"
        ""
    )
    ec.flip()
    ec.listen_presses()
    ec.wait_secs(wait_dur)
    pressed = ec.get_presses(relative_to=0.0)
    ec.write_data_line("listen / wait / get_presses relative_to 0.0", pressed)
    if not len(pressed):
        message = "no keys pressed"
    else:
        message = [
            f"{key} pressed at {round(time, 4)} secs\n" "" for key, time in pressed
        ]
        message = "".join(message)
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

    ###########################################
    # listen_presses / while loop / get_presses
    disp_time = wait_dur
    countdown = ec.current_time + disp_time
    ec.call_on_next_flip(ec.listen_presses)
    ec.screen_text(
        "press some keys\n\nlisten_presses()" f"\nwhile loop {disp_time}\nget_presses()"
    )
    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(
                "press some keys\n\nlisten_presses() "
                f"\nwhile loop {disp_time}\nget_presses()"
            )
            ec.flip()
    pressed = ec.get_presses()
    ec.write_data_line("listen / while / get_presses", pressed)
    if not len(pressed):
        message = "no keys pressed"
    else:
        message = [
            f"{key} pressed after {round(time, 4)} secs\n" "" for key, time in pressed
        ]
        message = "".join(message)
    ec.screen_prompt(message, msg_dur)
    ec.wait_secs(isi)

    #####################################################################
    # listen_presses / while loop / get_presses, relative to master clock
    disp_time = wait_dur
    countdown = ec.current_time + disp_time
    ec.call_on_next_flip(ec.listen_presses)
    ec.screen_text(
        "press some keys\n\nlisten_presses()\nwhile loop "
        f"{disp_time}\nget_presses(relative_to=0.0)"
    )
    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(
                "press some keys\n\nlisten_presses()\nwhile "
                f"loop {disp_time}\nget_presses(relative_to=0.0)"
                ""
            )
            ec.flip()
    pressed = ec.get_presses(relative_to=0.0)
    ec.write_data_line("listen / while / get_presses relative_to 0.0", pressed)
    if not len(pressed):
        message = "no keys pressed"
    else:
        message = [
            f"{key} pressed at {round(time, 4)} secs\n" "" for key, time in pressed
        ]
        message = "".join(message)
    ec.screen_prompt(message, msg_dur)

ea.plot_screen(screenshot)

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

Gallery generated by Sphinx-Gallery