[comp.windows.ms.programmer] detecting user inactivity

bruceh@hpsciz.sc.hp.com (Bruce Haines) (06/11/91)

	Any ideas on how to sense user "inactivity".  I'd like to set a
	timer that would go off if a user hasn't generated any keyboard/mouse
	input. Like whatever a screen saver app must do...?

risto@tuura.UUCP (Risto Lankinen) (06/12/91)

bruceh@hpsciz.sc.hp.com (Bruce Haines) writes:

>	Any ideas on how to sense user "inactivity".  I'd like to set a
>	timer that would go off if a user hasn't generated any keyboard/mouse
>	input. Like whatever a screen saver app must do...?

Hi!

The 'idle-detecting' message loop may suit your case.  That is, replace the
standard 'while( GetMessage() ) ... ' in the WinMain() with the following:

   while(TRUE)
      if( PeekMessage(&msg) )
      {
         // The queue contains messages - process them
         // GetMessage() would automatically detect WM_QUIT, but here we
         // must explicitly check for it.
         if( msg.message==WM_QUIT ) break;
         if( TranslateAccelerator(hWnd,hAcc,&msg) ) continue;
         TranslateMessage( &msg );
         DispatchMessage( &msg );
         // You might want to save the last time a message was processed
         dwLastMsgTime = GetTickCount();
      }
      else
      {
         // The queue is empty - user is doing nothing with *this* app
         if( GetTickCount()-dwLastMsgTime > MSGTIMEDELTA )
         {
            // Do something funny
         }
      }
   return msg.wParam;

Note, that the user might be doing something with *other* apps, so some
caution is necessary, if this will be used for a screen saver.  Also
note, that the GetTickCount() eventually wraps around, if the computer
is kept powered for a long period of time.

Terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              3   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 +1 is PRIME!  Now working on 2 -1 *
replies: risto@yj.data.nokia.fi     ***************************************

bonneau@hyper.hyper.com (Paul Bonneau) (06/13/91)

In article <17190004@hpsciz.sc.hp.com> bruceh@hpsciz.sc.hp.com (Bruce Haines) writes:
>
>	Any ideas on how to sense user "inactivity".  I'd like to set a
>	timer that would go off if a user hasn't generated any keyboard/mouse
>	input. Like whatever a screen saver app must do...?

One approach that springs to mind is to write a .dll and
install a system-wide windows hook in it (SetWindowsHook()
type WH_GETMESSAGE since user generated events are posted to
the event queue).  Each time you get a user-generated event,
call GetTickCount() and store the result in a static.  Before
doing so, however, compare the returned value with the old
contents of the static, and if the elapsed time is greater
than your threshold, install your timer.

This is a pretty rough sketch, but it should work.

cheers - Paul Bonneau.

jeff@extro.ucc.su.OZ.AU (Jeff Smartt) (06/14/91)

In article <17190004@hpsciz.sc.hp.com> bruceh@hpsciz.sc.hp.com (Bruce Haines) writes:
>
>	Any ideas on how to sense user "inactivity".  I'd like to set a
>	timer that would go off if a user hasn't generated any keyboard/mouse
>	input. Like whatever a screen saver app must do...?

G'day,

create a DLL that used a system hook that looks for WM_CHAR, WM_SYSCHAR, and
WM_MOUSEMOVE messages to any application.  Add a timer so that you can detect
the lack of messages and that's it!  See the hooks overview in the manual.

Note that the code MUST reside in a DLL - not a standard app.

Regards,
Jeff.
-- 
Jeffrey Smartt, Smartt Designs Pty. Ltd.
G.P.O. Box 619, Sydney, NSW, Australia, 2001.
Ph: +61-2-411-1910.
--------------------------------------------------------------------------------