?
Solved

Detect idle keyboard and mouse in MFC.

Posted on 2002-04-06
3
Medium Priority
?
555 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 1200 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

762 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