Time Tortoise: Idle Time Detection with MouseKeyHook


This is one in a series of articles about Time Tortoise, a Universal Windows Platform app for planning and tracking your work schedule. For more on the development of this app and the ideas behind it, see my Time Tortoise category page.

A practical time tracker needs to take into account user idle time, but it isn’t possible to do that using UWP. So for the past few weeks, I have been working on a companion app that will provide the main UWP app with information, like idle time, that it can’t get on its own. The SignalR-based communication infrastructure is now done, so this week I worked on the idle time detection.


For a time tracker, usable idle time detection depends on being able to subscribe to all mouse and keyboard events globally (not just for the current app). Once you solve that problem, what remains is this simple algorithm:

when a mouse or keyboard event fires
    set last user activity time to current time

User idle time is the difference between the current time and the last user activity time.

To track mouse and keyboard activity globally, I’m using an open source component, MouseKeyHook by George Mamaladze. It’s available on GitHub and as a Nuget package.


MouseKeyHook abstracts away the details of mouse and keyboard activity monitoring, and just exposes mouse and keyboard events. For example, there are events for keyboard keys being pressed, the mouse pointer moving, mouse buttons being clicked, and even the mouse scroll wheel turning.

In TimeTortoise.Server, the assembly that handles functionality requiring the .NET Framework 4.x, I added a class called IdleTime to keep track of when the user last used the mouse or keyboard. It contains a Subscribe() function that subscribes to 11 keyboard and mouse events, so that when one of these events fires, a method in IdleTime is called.

Each of the 11 methods calls the same local method, UpdateLastUserActivityTime(). That method updates a local DateTime variable to the current date/time. This allows the IdleTime class to provide information to its consumers about how long it has been since the user was active.


The MouseKeyHook code is in a class library that is consumed by Time Tortoise Companion, a WPF app whose only UI is an icon in the taskbar notification area. But since MouseKeyHook is capable of monitoring global mouse and keyboard events, this hidden companion app gets accurate information even though it is usually not consuming the user’s mouse and keyboard activity.

Last week, I tested SignalR communication functionality by sending the current system time from a WPF-based server to a UWP-based test console app. Now that TimeTortoise.Server has information about the user’s idle time, I can send that instead.

As a test, I ran Time Tortoise Companion, and then started the TimeTortoise.Console test app. Once per second, the test app displayed the number of seconds since the last user input. I verified that when I used the mouse or keyboard, the seconds reset to 0.

Next Steps

With idle time detection in place and available in the UWP environment, it’s now possible for Time Tortoise to stop timing an activity when the user steps away from the computer. When they return, they can decide whether to include or exclude their away time.