Red-Green-Code

Deliberate practice techniques for software developers

  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter

Time Tortoise: Idle Time Detection with MouseKeyHook

By Duncan Smith Jul 6 0

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.

MouseKeyHook

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.

IdleTime

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.

Testing

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.

Categories: TT

Prev
Next

Stay in the Know

I'm trying out the latest learning techniques on software development concepts, and writing about what works best. Sound interesting? Subscribe to my free newsletter to keep up to date. Learn More
Unsubscribing is easy, and I'll keep your email address private.

Getting Started

Are you new here? Check out my review posts for a tour of the archives:

  • • 2015 Summer Review
  • • 2015 in Review
  • • 2016 in Review
  • • What I Learned Working On Time Tortoise in 2017
  • • Competitive Programming Frequently Asked Questions: 2018 In Review

Archives

  • February 2019 (2)
  • January 2019 (5)
  • December 2018 (4)
  • November 2018 (4)
  • October 2018 (5)
  • September 2018 (4)
  • August 2018 (5)
  • July 2018 (4)
  • June 2018 (4)
  • May 2018 (5)
  • April 2018 (4)
  • March 2018 (4)
  • February 2018 (4)
  • January 2018 (5)
  • December 2017 (4)
  • November 2017 (5)
  • October 2017 (4)
  • September 2017 (4)
  • August 2017 (5)
  • July 2017 (4)
  • June 2017 (4)
  • May 2017 (5)
  • April 2017 (4)
  • March 2017 (5)
  • February 2017 (4)
  • January 2017 (4)
  • December 2016 (4)
  • November 2016 (5)
  • October 2016 (4)
  • September 2016 (4)
  • August 2016 (5)
  • July 2016 (4)
  • June 2016 (5)
  • May 2016 (4)
  • April 2016 (4)
  • March 2016 (5)
  • February 2016 (4)
  • January 2016 (4)
  • December 2015 (5)
  • November 2015 (4)
  • October 2015 (4)
  • September 2015 (5)
  • August 2015 (4)
  • July 2015 (5)
  • June 2015 (4)
  • May 2015 (4)
  • April 2015 (5)
  • March 2015 (4)
  • February 2015 (4)
  • January 2015 (4)

Recent Posts

  • What is Discrete Mathematics?
  • The Khan Academy Math Course System
  • Post-Mastery Practice Examples from Khan Academy
  • Using Khan Academy for Post-Mastery Practice
  • How Useful is Khan Academy for Learning Math?
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2019 Duncan Smith