Detect mouse movement or clicks anywhere in Windows

I'm working on a Touch-Screen app,
I want to have a timer and whenever the mouse moves or clicks I want to reset the timer (after 60 seconds or whatever a flash animation will be launched.)
LVL 13
Who is Participating?
Emmanuel PASQUIERFreelance Project ManagerCommented:
better yet , there is a windows function that will return the timer when the last mouse or keyboard action occurred.
function MilliSecondsIdle: DWord;
 liInfo: TLastInputInfo;
 liInfo.cbSize := SizeOf(TLastInputInfo) ;
 GetLastInputInfo(liInfo) ;
 Result := GetTickCount - liInfo.dwTime;

function SecondsIdle: DWord;
 Result := MilliSecondsIdle Div 1000;

procedure TForm1.tmrIdleTimer(Sender: TObject);
 lblLastInput.Caption:=Format('Last Input %f s',[MilliSecondsIdle/1000]);

Open in new window

you can intercept mouse messages by setting mouse hook. SetWindowsHookEx function with WH_MOUSE param. Here is samples:
rfwoolfAuthor Commented:
Looks like just what I need.
Thanks :)

Question will remain open a day or two in case.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

this example allow you to detect move, right up, right down, left up left down and wheel mouse evens
Alternatively, install your application as the screen saver and set the system to launch the screen saver after 60 seconds ;-)
rfwoolfAuthor Commented:
Do you know if that would work? Would the screen saver app terminate after mouse input?
I was just assuming that you wanted your programme to do something if there is a 60 sec inactivity, hence the suggestion to install your app as a screen saver instead.

Once Windows starts your "screen saver", it is up to you what you want to do with it... e.g. setting it to a maximised form and closing it in the OnMouseMove event, etc.
Emmanuel PASQUIERFreelance Project ManagerCommented:
Yes, creating a screen saver application would be the real thing.

Here is a complete example of a Delphi application turned into a screen saver :

Important things :
- how the app is detected by windows as being a Screen Saver : simply by putting it in \Windows directory and changing its extension to .SCR
- how Windows get the display name (for the control panel) : with a simple compiler directive
{$D SCRNSAVE My Delphi Screen Saver Name}
- how the application knows when launched that it must execute the screen saver or simply configure it : with a different command line parameter , "/s" for active mode and "/c" for configuration mode

other things explained in this example are more trivial, and you can read it or take the full source as a basis for yours
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.