Detect mouse move and key press event for all over the window screen

How to detect mouse move and key press event for all over the window screen ?
I want to achieve idle screen time functionality. Is there any better way to handle this scenario.