To responsibly add and use a hook we need to do three things. To quote MSDN, “A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure”. So in order to retrieve, or rather track, the time of the last input from the keyboard I had to use a hook. Unfortunately there’s no such function for individual input sources, such as the keyboard. The GetLastInputInfo method is great for checking when the system last received input from any source. Getting the time for last input from the keyboard You'll find this functionality in the AllInputSources class in the sample code. (Environment.TickCount - lastInputInfo.dwTime)) LastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo) In other words, we can get the date and time for the last input like this: var lastInputInfo = new LASTINPUTINFO() After the call the struct we called the method with will have it’s dwTime field set to the tick count when the last input was received by the system. When we call GetLastInputInfo we must call it with a LASTINPUTINFO whose cbSize variable is set to the size of the LASTINPUTINFO struct. Private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii) It will expect a struct called LASTINPUTINFO that we need to create as well. We can call GetLastInputInfo by creating a method with the extern keyword in a C# class. You are welcome to download the source code.įinding out the last time any input was given to the system no matter the source (keyboard, mouse etc) is fairly easy thanks to a function called GetLastInputInfo. As I found this experience rather interesting I’ve put together a little sample application that illustrates how to get the last time of any input to the system as well as the last time for input from the keyboard and the mouse respectively. NET into Windows that I’ve never done before. In order to do so I had to do some low-level hooks from. Mouse Nag detects when you go from using the keyboard to using the mouse no matter what application you are currently running. A couple of weeks ago I built a little Windows application called Mouse Nag with WPF.
0 Comments
Leave a Reply. |