Get mouse click while pressing key

URL of experiment:

Description of the problem:

Hello, I need your helps.
I am making a code in the ‘code’ component to get mouse click while participants pressing a certain key.
In specific, ‘s’, ‘d’, and ‘space’ keys are allowed, and participants are allowed to click on the image only when they are pressing ‘s’ and ‘d’. When the key is released, mouse clicks are blocked. When the key is pressed again, they can click on the image again. When ‘space’ key is pressed, the trial ends. Multiple mouse clicks are allowed while pressing a key. Multiple key presses and releases are also allowed in a trial unless ‘space’ key is pressed. How should I structure my code?

Current structure of my code is:
(Below is not written in python language

#if 's' or 'd'_is_pressed:
#    check if mouse is clicked in a valid clickable area
#    if mouse is clicked in a valid clickable area:
#        if key_pressed is 's':
#            do A
#        if key_pressed is 'd':
#            do B
#    if mouse is not clicked in a valid clickable area:
#        ignore the mouse click
#
#elif 'space' key is pressed:
#    continueRoutine = False

This is my general structure in ‘Each Frame’ tab, but I have no idea how I can check if key is being pressed. Can you help me with this issue?

How about checking for the mouse press first and then only checking the keyboard buffer (e.g. with keys = event.getKeys() ) if the mouse press is valid?

I think it’s working!! Thanks a lot.
I will share my code in case someone question the same topic.

## in Each Frame tab

if 1 in clkMouse.getPressed(): 
    # check if mouse click is inside valid clickable area
    if (clkMouse.x[-1] >= -cellsize*5) and (clkMouse.x[-1] <= cellsize*5) and (clkMouse.y[-1] >= -cellsize*5) and (clkMouse.y[-1] <= cellsize*5):
        clickFlag = 1
    else: 
        clickFlag = 0
 
    # mouse click was inside valid clickable area
    if clickFlag == 1:
        clickFlag = 0;

    # check keypress
        key = event.getKeys()
        if 's' in key:
            console.log('s pressed')

        elif 'd' in key:
            console.log('d pressed')
            
        else:
            console.log('ignore this mouse click')

elif not 1 in clkMouse.getPressed():
    console.log('wait for mouse click')