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:

  • 2023 in Review: 50 LeetCode Tips
  • 2022 in Review: Content Bots
  • 2021 in Review: Thoughts on Solving Programming Puzzles
  • Lessons from the 2020 LeetCode Monthly Challenges
  • 2019 in Review
  • Competitive Programming Frequently Asked Questions: 2018 In Review
  • What I Learned Working On Time Tortoise in 2017
  • 2016 in Review
  • 2015 in Review
  • 2015 Summer Review

Archives

Recent Posts

  • Do Coding Bots Mean the End of Coding Interviews? December 31, 2024
  • Another Project for 2024 May 8, 2024
  • Dynamic Programming Wrap-Up May 1, 2024
  • LeetCode 91: Decode Ways April 24, 2024
  • LeetCode 70: Climbing Stairs April 17, 2024
  • LeetCode 221: Maximal Square April 10, 2024
  • Using Dynamic Programming for Maximum Product Subarray April 3, 2024
  • LeetCode 62: Unique Paths March 27, 2024
  • LeetCode 416: Partition Equal Subset Sum March 20, 2024
  • LeetCode 1143: Longest Common Subsequence March 13, 2024
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2025 Duncan Smith