I have some code that is supposed to hit space when a red line runs over a white square. I have timed the whole function and it takes 3-4 milliseconds per loop. I also save every screen capture that is taken so I know exactly what each frame looks like and the one that the program triggers on, ie red line over white square. This is off loaded to a thread and the main loop only takes 40 microseconds or so to call it so that its not delaying the program. The Issue is that the frame that the program triggers the key press on is correct every time. However, the time that the keystroke registers in the video is delayed by 20-40 ish milliseconds. This is causing it to hit late occasionally.
I have tried keybd_event, send input, sending directly to the window. All of it has the delay. I don’t know if the issue is the OS taking to long processing the input, which I highly doubt, or if its the application itself that is slow to process the input. This is also somewhat unlikely as I have tried this with different video players and directly from the game itself, all of which have the same delay.
//starts time
std::chrono::steady_clock::time_point loop = std::chrono::high_resolution_clock::now();
//White square sufficiently occluded by red line
if (whites + 20 <= total) {
std::cout << "White: " << whites << " " << "Total: " << total << endl;
//hit space and release it
keybd_event(0x20, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x20, 0, KEYEVENTF_KEYUP, 0);
//clock the duration
std::cout << "key press: " << std::chrono::duration_cast<std::chrono::microseconds (std::chrono::high_resolution_clock::now() - loop).count() << endl;
Sleep(400);
exit(0); // to make it run once for testing
break;
}
Does anyone have any idea what might be causing this delay and potential solutions?
Mica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.