How do i make a code that holds keys?

I’m trying to make a code that works like described:
when ‘space’ is tapped do:
hold ‘d’ button (raw input), duration=117
hold ‘leftClick’, duration=117
after this ends:
hold ‘a’ button(raw input), duration=117
hold ‘leftClick’, duration=117
after the second one ends:
hold ‘d’ button(raw input), duration=117
hold ‘leftClick’, duration=117
loop
when ‘g’ is pressed:
break
Can someone make this code/tell me how to make it? I am new and I’m trying to make a code that would work for personal use, but it seems like there isn’t any tutorial/help on how to make a code that holds raw input keys.
Thanks for your help,
xnqs

You need to build a finite state machine. Start in a initial state where nothing is pressed. When in this state and the button is pressed for the entire duration, go to the next state. Then while in this state if the button is pressed for a duration, transfer to next state. Until you reach the final state.

You can make use of this library: keyboard · PyPI
Implement a while loop to execute the desired actions until ‘g’ is pressed. This would create a state machine where the final state is “pressing ‘g’”.