Detect idle keyboard and mouse in MFC.

I need a simple way to detect that the user has been idle(no keyboard and mouse activity) for a period of time in my application. I have several windows, dialog boxes and active X controls in my application so I don't think it is possible to just capture these messages in the main window and reset some timer. I'm thinking of implementing an application wide hook. Is this the best approach? Anyone got any source code for this?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

DanRollinsConnect With a Mentor Commented:
>>for a period of time in my application.

That article describes how to build a DLL for system-wide hook.  That is overkill if all you want to know is whether or not the user has ignored your app for some period of time.

In an App I wrote, I needed to hide the window and enforce a relogin after 15 minutes of inactivity.  I did it like this (not the code is substantially similar to that in the article, but it can be dome right in your own app; this is from my CMainFrame.cpp):

// Inactivity Timeout Handler
static HHOOK hMyKeyHook= 0;
static HHOOK hMyMouHook= 0;
static LRESULT CALLBACK MyKeyHookFunc(int code, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK MyMouHookFunc(int code, WPARAM wParam, LPARAM lParam);

void CMainFrame::EnableInactivityHook( BOOL fEnable )
     if ( fEnable ) {
          HTASK hTask= GetWindowTask( m_hWnd );
          hMyKeyHook=  SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)MyKeyHookFunc, AfxGetInstanceHandle(), (DWORD)hTask);
          hMyMouHook=  SetWindowsHookEx(WH_MOUSE,    (HOOKPROC)MyMouHookFunc, AfxGetInstanceHandle(), (DWORD)hTask);

          gdwLastActivity= ::GetCurrentTime();
          SetTimer( CNUM_InactivityTimer, CNUM_InactivityTimerTickMs, 0);
     else { // disable
          KillTimer( CNUM_InactivityTimer );
          if ( hMyKeyHook) UnhookWindowsHookEx( hMyKeyHook );
          if ( hMyMouHook) UnhookWindowsHookEx( hMyMouHook );
LRESULT CALLBACK MyKeyHookFunc(int code, WPARAM wParam, LPARAM lParam) {
     gdwLastActivity= ::GetCurrentTime();
     return CallNextHookEx( hMyKeyHook, code, wParam, lParam);
LRESULT CALLBACK MyMouHookFunc(int code, WPARAM wParam, LPARAM lParam) {
     gdwLastActivity= ::GetCurrentTime();
     return CallNextHookEx( hMyMouHook, code, wParam, lParam);
Note that there is no separate DLL.  And yes, this would do well wrapped in a nice class object.

I then have an OnTImer handler that compares the value of gdwLastActivity to GetCurrentTIme(), like so:

     if ( nIDEvent == CNUM_InactivityTimer ) {
          int nLogoffDelayMins= gpDoc->m_crsUser.m_nLogoffDelay;
          BOOL fAutoLogoff=     gpDoc->m_crsUser.m_fAutoLogoff;
          if ( fAutoLogoff && !gfLocked ) {
               if ( nLogoffDelayMins < CNUM_MinLogoffDelayMins ) nLogoffDelayMins= CNUM_MinLogoffDelayMins;

               DWORD dwLogoffTime= nLogoffDelayMins * 60000L;
               dwLogoffTime += gdwLastActivity;
               if ( ::GetCurrentTime() > dwLogoffTime ) {
                    DoLockProgram( FALSE /*fBringToFront*/ );

I had to perform some shenanagins in my DoLockProgram to prevent or allow loss of focus (I want my "Please Log In" screen to be the active foreground app after I hide my mainframe), but that is just an implementation detail.

-- Dan
Check - it has all the info that you need, including sample code. (Credits to robpitt for mentioning this in another thread)
jdrescherAuthor Commented:
Thanks, Dan. This exactly what I wanted..

All Courses

From novice to tech pro — start learning today.