unit Unit1;
interface
uses
Windows, SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Messages, AppEvnts;
type
TForm1 =
class(TForm)
tmr1: TTimer;
aplctnvnts1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
procedure aplctnvnts1Message(
var Msg: tagMSG;
var Handled: Boolean);
private
FLastUserInput: Cardinal;
FUserIsAfk: Boolean;
function GetTimeSinceLastTick: Cardinal;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
UserIsAfkIn = 5000;
// ms
procedure TForm1.aplctnvnts1Message(
var Msg: tagMSG;
var Handled: Boolean);
begin
case Msg.
Message of
WM_KEYFIRST..WM_KEYLAST, WM_MOUSEFIRST..WM_MOUSELAST:
if Msg.
Message <> WM_MOUSEMOVE
then
FLastUserInput := GetTickCount;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FLastUserInput := GetTickCount;
FUserIsAfk := false;
Caption := '
state green';
end;
function TForm1.GetTimeSinceLastTick: Cardinal;
begin
if GetTickCount < FLastUserInput
then
Result := (High(Cardinal) - FLastUserInput) + GetTickCount
else
Result := GetTickCount - FLastUserInput;
end;
procedure TForm1.tmr1Timer(Sender: TObject);
begin
if FUserIsAfk
then
begin
if GetTimeSinceLastTick < UserIsAfkIn
then
begin
Caption := '
User is back';
FUserIsAfk := false;
end;
end
else
begin
if GetTimeSinceLastTick >= UserIsAfkIn
then
begin
Caption := '
!!! Zomg! User stopped workin and idles around !!!';
FUserIsAfk := true;
end;
end;
end;
end.