Link to home
Start Free TrialLog in
Avatar of julianpointer
julianpointer

asked on

Away from PC/Online functionality like MSN Messenger

I need to have away from PC/Online  functionality like MSN Messenger, does anybody know an easy check to see if the user if active, mouse/keyboard activity.
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

It might be better to have some sort of timer event and a global variable that you set if no activity has happened for a keyboard/mouse event over a period of time. Then you just check the state of the global variable to see if the user is away or not.

In the code below the timer is set to run every second

// Global
tmrCheckIdle: TTimer;
dtLastActive: TDateTime;
iPeriod: Integer;  // some period of time you want to check

// timer event check
procedure tmrCheckIdleTimer(Sender: TObject);
var
  tTimeNow: TTime;
  wHour, wMin, wSec, wMSec: Word;
begin
  tTimeNow:= Time;
  // get the current time and break it down
  DecodeTime(dtLastActive, wHour, wMin, wSec, wMSec);
  // check if the period has been met.
  if (EncodeTime(wHour + iPeriod, wMin, wSec, 0) <= tTimeNow) then
  begin
    // disable the timer.
    tmrCheckIdle.Enabled:= False;
    // set some inactive variable
    bInactive := True;
  end;
end;

procedure AppMessages(var Msg: TMsg; var Handled: Boolean);
begin
  // check which messages are to be processed
  case msg.message of
    // just handle keyboard and mouse events.
    WM_KEYFIRST..WM_KEYLAST, WM_MOUSEFIRST..WM_MOUSELAST: begin
      // get the current time
      dtLastActive:= Time;
    end;
  end;
end;

Someone might have a better solution but I think this should do what you want.
O you need to do tmrCheckIdle.Enabled := True once the user starts to do something again
ASKER CERTIFIED SOLUTION
Avatar of fidel83
fidel83

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fidel83
fidel83

thanks Julian :-)
Never heard of this myself >TLastInputInfo< but will try it myself
Ah I notice that your version is a system wide time check.
But surely if the user clicks on another application the application you are trying to timecheck will not have the correct values.
Yeah that's how msn works - even if you're in another program, you are still not idle. It's when you dont move the mouse or keyboard at all does it go into idle mode...
If you were doing an "away from application" rather than "away from PC" functionality, what method would you use instead?
Yours looks pretty good - checking for windows messages...
ok great thanks.
Ill note yours down for future use though .. nice bit of code