![]() HHookMouse = SetWindowsHookEx(( int ) HookType. KbdHookProcedure = new HookProc (KbdHookProc) MouseHookProcedure = new HookProc (MouseHookProc) Return CallNextHookEx(hHookKbd, nCode, wParam, lParam) Static public int KbdHookProc( int nCode, IntPtr wParam, IntPtr lParam) Return CallNextHookEx(hHookMouse, nCode, wParam, lParam) user is active, at least with the mouse Static public int MouseHookProc( int nCode, IntPtr wParam, IntPtr lParam) Added all the hook types here just for reference Public static extern int CallNextHookEx( int idHook, int nCode, Use this function to pass the hook information to next hook procedure in chain. Public static extern bool UnhookWindowsHookEx( int idHook) Call this function to uninstall the hook. Public static extern int SetWindowsHookEx( int idHook, HookProc lpfn, Auto,ĬallingConvention = CallingConvention. [ DllImport ( "user32.dll", CharSet = CharSet. Use this function to install thread-specific hook. Declare KbdHookProcedure as HookProc type. Public event HookProc MouseHookProcedure Declare MouseHookProcedure as HookProc type. Public delegate int HookProc ( int nCode, IntPtr wParam, IntPtr lParam) Public class ClientIdleHandler : IDisposable I added some code into the Dispose() method of the form, to help close the hooks, like this: change to IDLE and stop counting seconds Private void timer1_Tick( object sender, EventArgs e) Private const int IDLE_THRESHOLD = 10 //how many seconds should pass before the app goes into idle modeĬlientIdleHandler = new ClientIdleHandler () Private int iIdleCount = 0 //keeps track of how many seconds the user has been idle Private ClientIdleHandler clientIdleHandler = null Also note that this code is not optimized in anyway and you may want to implement the static bActive variable different, perhaps raise events from the ClientIdleHandler class instead of using a timer like I do. ![]() NET 2.0 even though it seems to work well. This code has been running pretty fine for the last 2 years, but on. Note: This code has not been tested in production systems and if you decide to use it, do that at your own risk. I also added a progress bar on it (0 to 10) to give a visual indication on the idle seconds count. Each time the timer fires, it checks the bActive value in the ClientIdleHandler to see if the user has been active since the last check. First a look at the sample Windows Form which is made up of a lable indicating if the user is idle or active, and a timer set to 1 second. This code is actually a piece of cake to implement if you use the ClientIdleHandler class (code below). That's why I implemented the IDispose pattern for it. Obviously this involves a couple of DLL Import statements and you have to be careful to UnHook everything when the application exits as we're dealing with unmanaged code here. So the way to do it is to set up a coupld of low-level Windows hooks to detect mouse and keyboard activity. I used it on a chat client I developed because we were not allowed to use Messenger at the site. I thought I could share some piece of code I wrote a couple of years ago to detect if a user is idle or not.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |