Solved

Detect idle keyboard and mouse in MFC.

Posted on 2002-04-06
3
544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is AWS.RDS considered a part of "Big Data"? 4 132
How to convert MFC::CString to UTF8 wchar_t* 10 386
Post a good COM tutorial 1 68
Dialogbox API leak? 18 109
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

733 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