Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Press and hold key for set duration before continuing sequence

Hi there,

I'm new to OpenSesame and Python but I've been asking some progress with a few threads and the key release is working fine but I'm having some trouble setting a 'press and hold' response that is needed to start the sequence.

The experiment is quite simple:

  1. Participant has to press and hold a key for 1000ms
  2. Three discs appear on the screen
  3. One of the disk turns green, release the button press, and make a reaching motion to touch the location on the touchscreen

I am struggling to set the press and hold response. I've searched the topics from a similar topic and I was trying to figure out a way to adapt this inline from an earlier topic:

I just don't have enough knowledge of python to know how to go about it.

Any help is much appreciated.


  • Hi,

    The problem with your script is the indentation, i.e. the number of tabs that precede each line. The break statement should be intended one step down, so that it's not in the else block. Does that make sense? Like this:

    import pygame
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:

    An equivalent code that is perhaps easier to read would be:

    import pygame
    while not any(event.type == pygame.KEYDOWN for event in pygame.event.get()):

    To learn a bit more about loops, check out this video:


    There's much bigger issues in the world, I know. But I first have to take care of the world I know.

  • That makes perfect sense, Sebastiaan. The video was quite helpful but I am still struggling to implement the hold component for this task.

    I can get an approximation by setting the duration of a sketchpad for 3995ms (we've increased the length of the press). However, I need to be able to reset the trial if the key is released early. I need the key release time before a reaching response is performed to the target on a touchscreen.

    I have been trying to adapt some of your code from an earlier post for measuring key press durations within a while loop plus an else loop, combined with another post that used items.execute() command to break from a component if response incorrect. I can't quite get it to work and I think I may have the logic/structure of the loop confused or maybe I need a coroutine?

    I have included a screenshot of the structure of the experiment and the code element I have been working on.

    My intention was something along these lines:
    While a key is held down for less than 4 seconds, draw a canvas with some text and a prompt image. Get the time and update key_down_time. Then break/continue with the next component.

    Else: if the key is released before the 4s time limit then reduce the trial_loop count by 1 and execute the reset_loop.

    Begin trial again

    Apologies for the long-winded nature of my query. Any help is much appreciated.


  • edited January 18

    Hi there,

    I have got the task working in terms of basic functionality. Ideally, it would be good if I could implement a coroutine that exits if a key release is made during the presentation of two sequential sketchpads to catch early releases.

    I have tried to adapt the information about creating custom coroutines that terminate with a response but it does not register for the key release - I suppose because the return does not include a 'key release response' component. I am not sure how to implement that.

    At the minute I have the following in place:

    1. inline: defining the key release generator
    2. coroutines: sketchpad hold (3995ms)
    3. coroutines: sketchpad: show three stimulus positions (variable delay; ~750 - 2500ms)

    If there's no key release by the end of coroutine, coroutine should exit to continue sequence. If there is a key release during coroutine then it should exit to a 'Too early' feedback screen and reset or repeat that trial.

    An example, or suggestions for how to proceed would be greatly appreciated.


Sign In or Register to comment.