How to find out if any button on the keyboard was pressed to determine the user's activity (that it is not afk)?

It is ideal to take a fingerprint of the keyboard through GetKeyboardState and compare with the previous fingerprint, but it is limited to the application. Is there an analogue or asynchronous version of the function for global validation of a key?

The function for one GetKeyState button has a global version of GetAsyncKeyState . In GetKeyboardState in the standard form there is no such alternative. Perhaps there is some kind of samopnaya function?

Using a DLL trap for such a trifle seems wasteful and you don’t want to understand a whole DLL because of this + a possible fight against antivirus in this case.

  • You can call the GetAsyncKeyState function for each key (only 256 times). Only this function is not a panacea, because if another application calls it in the interval between your calls, you will not get reliable information about the keystroke. - zed
  • Here the implementation may interfere - it is planned to check the activity in the timer, whose interval is 1 second. 256 times every second, they can either not fit into it, or the performance functionality will be overloaded, given that other functions are performed in the same timer .. - Droltromed
  • On the implementation of GetAsyncKeyState * 256 was a thought. But the question is: is the execution of GetAsyncKeyState * 256 identical in speed with GetKeyboardState ? - Droltromed
  • one
    So test QueryPerformanceCounter to help. - zed
  • @zed, thanks for the QueryPerfomanceCounter . Checked through it. Execution duration in seconds: GetAsyncKeyState * 256 = 0,0000661240 with GetKeyboardState = 0,0000021136 It turns out that there is still a difference, even if it is not even a fraction of a second. Used the code: //каждая отдельно for i:=0 to 255 do a[i]:=GetAsyncKeyState(i); //общий слепок GetKeyboardState(State); //каждая отдельно for i:=0 to 255 do a[i]:=GetAsyncKeyState(i); //общий слепок GetKeyboardState(State); - Droltromed

2 answers 2

GetLastInputInfo Retrieves the time of the last input event.

And here is an example of using http://www.cyberforum.ru/delphi-beginners/thread1237003.html

The minimum version of Windows 2000 (Win 98 will not work).

Well, the English discussion: https://stackoverflow.com/questions/1442246/how-to-get-the-last-windows-active-time-by-windows-api

  • Thanks for the tip. I tried, but GetLastInputInfo always returns False to 64-bit, I did not try to 32-bit. Code: LInput: TLastInputInfo; GetLastInputInfo(Linput); //возвращает False Linput.dwTime; //возвращает 0, потому что функция не отработала LInput: TLastInputInfo; GetLastInputInfo(Linput); //возвращает False Linput.dwTime; //возвращает 0, потому что функция не отработала LInput: TLastInputInfo; GetLastInputInfo(Linput); //возвращает False Linput.dwTime; //возвращает 0, потому что функция не отработала . I tried CallNtPowerInformation - it returns data mainly on system load activity, and not on idle time, unfortunately. But I will try to further explore both functions. Perhaps something is wrong. - Droltromed
  • The CallNtPowerInformation function in the returned result is t.TimeRemaining=4294967295 . This is definitely not downtime. I tried to call her in half a minute, she returned 30 less. It looks like this is a time to shutdown. There is no system shutdown condition in the system settings, so perhaps the number is so large. It can probably be used to compare how many seconds there was no activity. - Droltromed
  • Understood, helped. Thank. The difference in TimeRemaining determines how many seconds no activity has been shown in the system. True, the result is updated in the function about once every half minute-minute. Of course, not an analogue of the asynchronous copy of the keyboard, but in some ways it is even better. - Droltromed

Here someone heard about SetWindowsHookEx with WH_KEYBOARD_LL hook? Works globally. No export traps. I once did this on VB6 ... Consider the substitution of the keyboard driver

  • It still turns out the same trap, just without using a DLL. It may be useful for global keyboard reading, but too cumbersome for a simple activity check task. Plus, the traps are not always kind of antiviruses. And so to work as an analogue of the global KeyboardState, I think, is quite effective. - Droltromed