Hold, and release on demand

I would like to control hold ->release action with a hotkey, is it possible?
Not sure how it could be done with W3C tho, it seem the duration has to be set before the key is pressed.

For example: the target program is running in a emulator. When I press space, the key is longpressed. When I release space, the longpress ends.