Press and release the spacebar as a response

Hello everyone. I am creating a stop signal task that involves a holding response. Initially, a fixation stimulus appears, and the subject must start pressing a key (spacebar) to proceed to the next stimulus. Then, there is an image stimulus to which the subject must continue holding down. If the subject lifts the finger during this phase, it is considered an error, and the routine is interrupted. Subsequently, another stimulus appears, and the subject must respond by either lifting the finger or, in some cases, keeping it pressed on the key. I have tried to implement some code, but I am facing difficulties. I appreciate anyone who can help me.

The key is to have separate keyboard responses listening for space press and space release.