Use a timer and the mouse move over the application...

Something like, if there isn't any movement over the application GUI, start the timer, if mouse move, restart the timer, if not just put a limit to make the application logoff.