Auto application logout
-
I've got an MFC based database application (built with VS.NET 2002) with a login feature. I want to detect if the user hasn't interacted with the app for a certain timeout period. When I detect this "event", I want to either exit the application, OR lock-out the user and force them to type in a password again. I can handle the code for a timer handler to check flags, and do the exit or login. But how to detect keypresses/mouse movement? I want to avoid rewrites to each of my dialogs and main app. Is there a way to hook into the app's message queue and detect keypresses and mouse activity for all child windows? Or any other simple way? TIA, Andrew
-
I've got an MFC based database application (built with VS.NET 2002) with a login feature. I want to detect if the user hasn't interacted with the app for a certain timeout period. When I detect this "event", I want to either exit the application, OR lock-out the user and force them to type in a password again. I can handle the code for a timer handler to check flags, and do the exit or login. But how to detect keypresses/mouse movement? I want to avoid rewrites to each of my dialogs and main app. Is there a way to hook into the app's message queue and detect keypresses and mouse activity for all child windows? Or any other simple way? TIA, Andrew
I did this with an application wide keyboard and mouse hook. Let me see if I can dig up the code. class CActivity { public: CActivity(); virtual ~CActivity(); public: static DWORD GetLastActivity(); static void SetLastActivity(DWORD dwActivity); public: static DWORD m_dwLastActivity; static HANDLE m_hMutex; }; extern CActivity g_Activity; template class CInactivityHook { public: CInactivityHook(); virtual ~CInactivityHook(); public: BOOL Start(DWORD dwThreadID); BOOL Stop(); protected: static LRESULT CALLBACK MyKeyHookFunc(int code, WPARAM wParam, LPARAM lParam); static LRESULT CALLBACK MyMouHookFunc(int code, WPARAM wParam, LPARAM lParam); protected: static HHOOK m_hMyKeyHook; static HHOOK m_hMyMouseHook; }; template HHOOK CInactivityHook::m_hMyKeyHook=NULL; template HHOOK CInactivityHook::m_hMyMouseHook=NULL; template CInactivityHook::CInactivityHook() { g_Activity.SetLastActivity(::GetCurrentTime()); m_hMyKeyHook=0; m_hMyMouseHook=0; } template CInactivityHook::~CInactivityHook() { Stop(); } template CInactivityHook::Stop() { if ( m_hMyKeyHook) { UnhookWindowsHookEx( m_hMyKeyHook ); m_hMyKeyHook = NULL; } if ( m_hMyMouseHook) { UnhookWindowsHookEx( m_hMyMouseHook ); m_hMyMouseHook = NULL; } return TRUE; } template CInactivityHook::Start(DWORD dwThreadID) { if ( !m_hMyKeyHook) { m_hMyKeyHook= SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)MyKeyHookFunc, AfxGetInstanceHandle(), (DWORD)dwThreadID); } if ( !m_hMyMouseHook) { m_hMyMouseHook= SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MyMouHookFunc, AfxGetInstanceHandle(), (DWORD)dwThreadID); } return TRUE; } template LRESULT CALLBACK CInactivityHook::MyKeyHookFunc(int code, WPARAM wParam, LPARAM lParam) { CActivity::SetLastActivity(::GetCurrentTime()); return CallNextHookEx( m_hMyKeyHook, code, wParam, lParam); } template LRESULT CALLBACK CInactivityHook::MyMouHookFunc(int code, WPARAM wParam, LPARAM lParam) { CActivity::SetLastActivity(::GetCurrentTime()); return CallNextHookEx( m_hMyMouseHook, code, wParam, lParam); } /////// // now in my cview CInactivityHook<0> CommonViewInactivity; void CCommonView::EnableInactivityHook( BOOL fEnable ) { CommonViewInactivity.Start(::GetCurrentThreadId()); SetTimer(DONE_DIGITIZING_TIMER,1000,NULL); CActivity::SetLastActi
-
I did this with an application wide keyboard and mouse hook. Let me see if I can dig up the code. class CActivity { public: CActivity(); virtual ~CActivity(); public: static DWORD GetLastActivity(); static void SetLastActivity(DWORD dwActivity); public: static DWORD m_dwLastActivity; static HANDLE m_hMutex; }; extern CActivity g_Activity; template class CInactivityHook { public: CInactivityHook(); virtual ~CInactivityHook(); public: BOOL Start(DWORD dwThreadID); BOOL Stop(); protected: static LRESULT CALLBACK MyKeyHookFunc(int code, WPARAM wParam, LPARAM lParam); static LRESULT CALLBACK MyMouHookFunc(int code, WPARAM wParam, LPARAM lParam); protected: static HHOOK m_hMyKeyHook; static HHOOK m_hMyMouseHook; }; template HHOOK CInactivityHook::m_hMyKeyHook=NULL; template HHOOK CInactivityHook::m_hMyMouseHook=NULL; template CInactivityHook::CInactivityHook() { g_Activity.SetLastActivity(::GetCurrentTime()); m_hMyKeyHook=0; m_hMyMouseHook=0; } template CInactivityHook::~CInactivityHook() { Stop(); } template CInactivityHook::Stop() { if ( m_hMyKeyHook) { UnhookWindowsHookEx( m_hMyKeyHook ); m_hMyKeyHook = NULL; } if ( m_hMyMouseHook) { UnhookWindowsHookEx( m_hMyMouseHook ); m_hMyMouseHook = NULL; } return TRUE; } template CInactivityHook::Start(DWORD dwThreadID) { if ( !m_hMyKeyHook) { m_hMyKeyHook= SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)MyKeyHookFunc, AfxGetInstanceHandle(), (DWORD)dwThreadID); } if ( !m_hMyMouseHook) { m_hMyMouseHook= SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MyMouHookFunc, AfxGetInstanceHandle(), (DWORD)dwThreadID); } return TRUE; } template LRESULT CALLBACK CInactivityHook::MyKeyHookFunc(int code, WPARAM wParam, LPARAM lParam) { CActivity::SetLastActivity(::GetCurrentTime()); return CallNextHookEx( m_hMyKeyHook, code, wParam, lParam); } template LRESULT CALLBACK CInactivityHook::MyMouHookFunc(int code, WPARAM wParam, LPARAM lParam) { CActivity::SetLastActivity(::GetCurrentTime()); return CallNextHookEx( m_hMyMouseHook, code, wParam, lParam); } /////// // now in my cview CInactivityHook<0> CommonViewInactivity; void CCommonView::EnableInactivityHook( BOOL fEnable ) { CommonViewInactivity.Start(::GetCurrentThreadId()); SetTimer(DONE_DIGITIZING_TIMER,1000,NULL); CActivity::SetLastActi
:cool: Think about writing an article for it :)
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
:cool: Think about writing an article for it :)
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Thanks. I have very little free time at the moment... John