C + + anti lock screen Demo

Generally, the computer in the company has a lock screen program. As long as the computer is not operated for a period of time, it will automatically lock the screen. For a while, I often need to walk away for a while. The computer always locks the screen. I have to input the password every time. I feel it's troublesome. I want to write a program to prevent the screen from locking.

The principle of automatic screen locking program should be to detect whether the system receives messages within the specified time. If no messages are received, the screen will be automatically locked. At the beginning, I tried using PostMessage, which was really not good. It seems that the lock screen program should detect the system message instead of the application message, so as long as the system message is sent at intervals, the automatic lock screen can be prevented. Search the Internet for the relevant API, find a SendInput function, simulate keyboard events, and read the following function description to write Demo.

 

  1. #include "stdafx.h"  
  2. #include <Windows.h>  
  3. #include <process.h>  
  4. #include <iostream>  
  5. #include <stdlib.h>  
  6.   
  7. using namespace std;  
  8. unsigned __stdcall InkeyBoardThread(void *param);  
  9.   
  10. int main(int argc, char* argv[])  
  11. {  
  12.     TCHAR consoleName[12] = __TEXT("MyHookTest");  
  13.     SetConsoleTitle(__TEXT("MyHookTest"));  
  14.   
  15.     HANDLE hThread = (HANDLE)_beginthreadex(NULL, NULL, InkeyBoardThread, (void*)consoleName, NULL, NULL);  
  16.     printf("Hello World!\n");  
  17.     CloseHandle(hThread);  
  18.   
  19.     char b;  
  20.     cin>>b;  
  21.     return 0;  
  22. }  
  23.   
  24. unsigned __stdcall InkeyBoardThread(void *param)  
  25. {  
  26.     INPUT myInput[4];  
  27.     myInput[0].type = INPUT_KEYBOARD;  
  28.     myInput[0].ki.dwFlags = 0;  
  29.     myInput[0].ki.wVk = VK_CAPITAL;  
  30.   
  31.     myInput[1].type = INPUT_KEYBOARD;  
  32.     myInput[1].ki.dwFlags = KEYEVENTF_KEYUP;  
  33.     myInput[1].ki.wVk = VK_CAPITAL;  
  34.   
  35.     myInput[2].type = INPUT_KEYBOARD;  
  36.     myInput[2].ki.dwFlags = 0;  
  37.     myInput[2].ki.wVk = VK_CAPITAL;  
  38.   
  39.     myInput[3].type = INPUT_KEYBOARD;  
  40.     myInput[3].ki.dwFlags = KEYEVENTF_KEYUP;  
  41.     myInput[3].ki.wVk = VK_CAPITAL;  
  42.   
  43.     while (TRUE)  
  44.     {  
  45.         SendInput(1, &myInput[0], sizeof(INPUT));  
  46.         SendInput(1, &myInput[1], sizeof(INPUT));  
  47.         SendInput(1, &myInput[2], sizeof(INPUT));  
  48.         SendInput(1, &myInput[3], sizeof(INPUT));  
  49.   
  50.         Sleep(300000);  
  51.     }  
  52. }  


The Demo above mainly simulates the Caps Lock keyboard event periodically. The reason for selecting the Caps Lock event is that the key will not interfere with other applications. If you use other keyboard events, such as' a ', if you open a txt edit box, the' a 'character will be entered in the edit box. Of course, we can use HOOK to intercept this key, but we also need to add processing functions, which is troublesome, so we won't write it here. If the thread simulating the event in the above Demo wants to exit normally, don't use Sleep. Use WaitForSingleObject to open an event or semaphore, and it's OK. No more verbose...

Tags: Windows

Posted on Tue, 31 Mar 2020 08:34:10 -0700 by evilren