Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Auto application logout

Auto application logout

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++databasevisual-studiodata-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew Allen
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • A Andrew Allen

      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

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • J John M Drescher

        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

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        :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"

        J 1 Reply Last reply
        0
        • R Ryan Binns

          :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"

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          Thanks. I have very little free time at the moment... John

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups