Solved

Detect idle keyboard and mouse in MFC.

Posted on 2002-04-06
3
539 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
ID: 6923167
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
ID: 6923215
>>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
ID: 6928933
Thanks, Dan. This exactly what I wanted..

John
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 6 69
EvenOdd challenge 10 127
how to use laptop or pad camera in vb.net windows application 2 86
Why use this lambda? 12 37
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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…
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…

829 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