Determine the idle time of the software (use Application.onmessage to filter all keyboard and mouse messages)

Source: Internet
Author: User

Getlastinputinfo is the detection system input, applied to a program is not appropriate!

There are two ways to monitor input messages for this problem:

1. Thread-level hooks, hook up mousehook and Keyboardhook

2. Do the processing in Application.onmessage

Obviously, the 2nd method is more convenient!

As we all know, the keyboard and mouse messages are queue messages, need to pass through the message queue after some processing, and then sent to the various wndproc. Based on this principle, we can do the message processing in the OnMessage, that is, declare the global variables, to save the last input action time, and then use the time control to do a timed scan, with GetTickCount to determine whether to timeout.

UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Appevnts, Extctrls, stdctrls;
    typeTForm1=class(tform) applicationevents1:tapplicationevents;    Tmr1:ttimer;    Edt1:tedit; procedureApplicationevents1message (varmsg:tagmsg; varHandled:boolean); procedureTmr1timer (Sender:tobject); Private    {Private Declarations}   Public    {Public Declarations}  End;varForm1:tform1; Elapsedtime:longint;Implementation{$R *.DFM}procedureTform1.applicationevents1message (varmsg:tagmsg; varHandled:boolean);begin   CaseMSG.message  ofWm_keydown,wm_lbuttondown,wm_lbuttonup://various keyboard and mouse messages    beginElapsedTime:=GetTickCount; //handled:=true; Do not set Handled to True here, otherwise the message will not be distributed to WndProc.    End; End;End;procedureTform1.tmr1timer (sender:tobject);begin   ifGettickcount-elapsedtime>60000  Then   beginTMR1. Enabled:=false; ShowMessage ('1 minutes, the software's not working.'); End;End;End.

Reference: http://www.cnblogs.com/key-ok/p/3358918.html

Determine the idle time of the software (use Application.onmessage to filter all keyboard and mouse messages)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.