我正在运行一个
while
循环,其中程序正在监听特定的键,比如按一次 Q 应该启动一个进程,再次按 Q 应该停止它,然后按 E 应该打破循环。
我用来实现这个的代码是:
input.type = INPUT_MOUSE;
while (true) {
if (printed_1 != true) {
cout << "Autoclicker Running" << endl;
printed_1 = true;
}
if (GetAsyncKeyState('Q') && started == false) {
if (printed_2 != true) {
cout << "Autoclicker Started" << endl;
printed_2 = true;
}
printed_3 = false;
click = true;
started = true;
}
if (GetAsyncKeyState('Q') && started == true) {
if (printed_3 != true) {
cout << "Autoclicker Stopped" << endl;
printed_3 = true;
}
printed_2 = false;
click = false;
started = false;
}
if (GetAsyncKeyState('E') && !exit_key_down) {
cout << "Exiting Program" << endl;
click = false;
break;
}
if (click == true) {
if (get<0>(button) == 1) {
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
} else if (get<0>(button) == 2) {
input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
} else if (get<0>(button) == 4) {
input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN;
}
SendInput(1, &input, sizeof(INPUT));
if (get<0>(button) == 1) {
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
} else if (get<0>(button) == 2) {
input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
} else if (get<0>(button) == 4) {
input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP;
}
SendInput(1, &input, sizeof(INPUT));
Sleep(1);
}
}
运行此代码时,我按 Q 并且第一个
if
语句运行,但不应该发生的是第二个 if
语句也紧随其后运行,停止进程。按 E 无论如何都有效。
运行此代码时的输出是:
Q被按下一次:
Autoclicker Started
Autoclicker Stopped
E被按下一次:
Exiting Program
我尝试添加其他布尔值以使
if
语句更准确。就像在前两个 bool toggle_key_down = false
语句中添加 if
,像这样:
if (GetAsyncKeyState('Q') && toggle_key_down == false && started == false) {
if (printed_2 != true) {
cout << "Autoclicker Started" << endl;
printed_2 = true;
}
printed_3 = false
click = true;
started = true;
toggle_key_down = true;
}
if (GetAsyncKeyState('Q') && toggle_key_down == true && started == false) {
if (printed_2 != true) {
cout << "Autoclicker Started" << endl;
printed_2 = true;
}
printed_3 = false
click = true;
started = true;
toggle_key_down = false;
}
我还尝试在
if
语句之间添加延迟,认为这可能是由于程序为两个 if
语句处理一个按键,像这样:
if (GetAsyncKeyState('Q') && toggle_key_down == false && started == false) {
if (printed_2 != true) {
cout << "Autoclicker Started" << endl;
printed_2 = true;
}
printed_3 = false
click = true;
started = true;
toggle_key_down = true;
}
Sleep(0.1);
if (GetAsyncKeyState('Q') && toggle_key_down == true && started == false) {
if (printed_2 != true) {
cout << "Autoclicker Started" << endl;
printed_2 = true;
}
printed_3 = false
click = true;
started = true;
toggle_key_down = false;
}
我已确保包含所有正确的库,但输出是相同的。
我应该怎么做才能解决这个问题?