Solved

Detect idle keyboard and mouse in MFC.

Posted on 2002-04-06
3
508 Views
Last Modified: 2013-11-25
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?
0
Comment
Question by:jdrescher
3 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmsj00/html/cpp0200.asp - it has all the info that you need, including sample code. (Credits to robpitt for mentioning this in another thread)
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 300 total points
Comment Utility
>>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*/ );
               }
          }
          return;
     }

=-=-=-==-=-=-=-=-=-
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
0
 
LVL 1

Author Comment

by:jdrescher
Comment Utility
Thanks, Dan. This exactly what I wanted..

John
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now