I am trying to make a C# program that detects inactivity, and I was trying to do this by checking the keyboard buttons, but Console.ReadLine()
can only detect keyboard inputs if they happen while in the window, how would I make it read the keyboard input without being inside the window.
Asked
Active
Viewed 56 times
2

shrek231
- 65
- 6
-
What kind of program are you creating? WinForms, WPF, Xamarin? – Paul Sinnema Nov 25 '20 at 23:34
-
Haven't tried this myself but my guess is you could create a Timer that is triggered every so many milliseconds. And add a Control.KeyPress event (in WinForms). If a key is pressed set a flag and test that flag in the Timer callback. In the Timer callback count the number of iterations without a key being pressed.You need to be aware that a Timer in fact uses Threading so your flag must be handled in a Thread safe manner using lock() or a Mutex and volatile variables. Read up on Threading before you attempt this. – Paul Sinnema Nov 25 '20 at 23:45
-
@PaulSinnema I believe this is for a console application as he referenced `Console.ReadLine()` – Hayden Nov 25 '20 at 23:49
-
@shrek231 Does [this link](https://stackoverflow.com/questions/24411887/how-to-send-keys-data-to-a-console-window-without-having-focus) answer your question? – Hayden Nov 25 '20 at 23:52
-
[This](https://stackoverflow.com/questions/1037595/c-sharp-detect-time-of-last-user-interaction-with-the-os) may answer your question. – Connell.O'Donnell Nov 26 '20 at 00:37
-
@Hayden that did answer my question, but how do i close the question now that its answered? im new to stackoverflow – shrek231 Nov 26 '20 at 21:14