i wouldnt do it this way. Why? it is a hack may not work all the time, can annoy the user, etc.

there are proper ways to tell the machine it should not go to sleep. Google it