How to do key press and key release in script please?

You can modify my addon, that does that, but for switching between various tools.

It uses something called event filter, that is installed on the Qt’s mdiArea (basically the canvas in tabs mode, and place where subwindows are displayed).
It catches all the events going on, and you can do some stuff on events, that come from key pressing and releasing (two different events for that).

What’s worth noting, is that those events don’t occur, if a shortcut is set to that key, and events are being sent multiple times for each press and release (I solved that by counting time elapsed from previous event like that, and using only those that happened long after the other, but for simple use like yours without any toggling, maybe it won’t be necessary).