in reply to turning off and on a program

If this program is running in the background (i.e. does not have focus), you'll need to somehow intercept all keypresses, passing them along to the operating system unless they're one of your control sequences.

I'm not entirely sure how one would do this - there may be no easy way. You should browse through the Windows modules on CPAN to see if you can find anything useful.