When you enter the number of seconds begins to load the processor.

#include <iostream> #include <Windows.h> using namespace std; int main() { int time,x,y; bool state = false; cout << "Time between clicks(in ms): "; cin >> time; cout << "Press F12 to activate / deactivate the script."; POINT mouse; while(true) { if (GetAsyncKeyState(VK_F12)) { if (state == false) { state = true; Sleep(250); } else { state = false; Sleep(250); } } if (GetKeyState(VK_LBUTTON) & 0x8000 && (state == true)) { GetCursorPos(&mouse); mouse_event(MOUSEEVENTF_LEFTUP,mouse.x,mouse.y ,0,0); mouse_event(MOUSEEVENTF_LEFTDOWN,mouse.x,mouse.y ,0,0); Sleep(time); } if (GetKeyState(VK_RBUTTON) & 0x8000 && (state == true)) { GetCursorPos(&mouse); mouse_event(MOUSEEVENTF_RIGHTUP,mouse.x,mouse.y ,0,0); mouse_event(MOUSEEVENTF_RIGHTDOWN,mouse.x,mouse.y ,0,0); Sleep(time); } } } 

What can be done in this situation?

  • Unload the program using Sleep inside the cycle for a couple of milliseconds. By the way, you have Sleep present, but it is not constantly called, but you need to call either each cycle (the easiest way), or at least once every 100 cycles. Add Sleep(1) after while - you will immediately see the result. - nick_n_a
  • when I use sleep, then there is a delay in the click - Kirill Panfilov
  • and sleep is called for ordering between clicks - Cyril Panfilov
  • You have two solutions to the problem 1) Call Sleep every 10 cycles, or every 100 cycles, otherwise your program will take 100%. 2) call synchronous functions work with clave. (I think it does not suit you, you took something asynchronous) - nick_n_a
  • And you can not make expectations of action without a cycle? - Kirill Panfilov

1 answer 1

You do not need to call GetAsyncKeyState in a loop only for the purpose of global capture of keyboard or mouse clicks. With this method, your program always either loads the processor too much, or reacts to events not fast enough. Instead, use a specially designed mechanism for this, the Low-level keyboard (mouse) hook:

 #include <stdio.h> #include <stdlib.h> #include <Windows.h> bool state = false; LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { switch (wParam) { case WM_KEYDOWN: PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam; if(p->vkCode == VK_F12) { state = !state; } break; } } return CallNextHookEx(NULL, nCode, wParam, lParam); } LRESULT CALLBACK LowLevelMouseProc( int nCode, WPARAM wParam,LPARAM lParam){ if (nCode == HC_ACTION) { switch (wParam) { case WM_LBUTTONDOWN: if(state != false) { //Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Π΅ дСйствия ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ ΠΌΡ‹ΡˆΠΈ... } break; } } return CallNextHookEx(NULL, nCode, wParam, lParam); } int main(int argc, char* argv[]) { printf("Press F12 to activate / deactivate the script."); HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0); if(hook == NULL) { printf("Failed to install keyboard hook!\n"); getchar(); return 1; } hook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, 0, 0); if(hook == NULL) { printf("Failed to install mouse hook!\n"); getchar(); return 1; } MSG msg; while (!GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }