I want to pause my program on the 0x32, 0x33 and 0x34 key and make it work again on the 0x31 key, how can I? I used this code to pause on the 0x32 key, it's working, but I can't get it back to work on the desired key
2 Answers
To summarize what @user4581301 suggested:
#include <conio.h> ... if (GetAsyncKeyState(0x32) || GetAsyncKeyState(0x33) || GetAsyncKeyState(0x34)) { while (_getch() != 0x31) ; } 6 Comments
7VK
to unpause on the 0x31 key, how do I?
Vlad Feinstein
@7VK sorry for the typo; fixed now.
7VK
is pausing, now I would just like to make it unpause and work again on the 0x31 key, the pause keys would like it to be 0x32, 0x33 and 0x34, and the unpause key to be 0x31
Vlad Feinstein
@7VK I've modified the code to pause on either 0x32, 0x33 or 0x34, and resume on 0x31. Does it work for you now?
7VK
it's just pausing, it's not returning to work on key 0x31
|
If your application is a console application, I implemented the function you want through loop. If it's a desktop application, you can refer to my code logic.
#include <iostream> #include<cstdlib> #include<time.h> #include<Windows.h> #include<conio.h> using namespace std; int main() { while (true) { for (int i = 0; i < 5; ++i) { if (GetAsyncKeyState(gun_keys[i]) && (gun != guns[i])) { gun = guns[i]; system("cls"); gun_delay = GetTime(gun->rpm); gun_index = 0; break; } } if (GetAsyncKeyState(VK_DELETE)) //Bind key, what close this program { ExitProcess(-1); //Exit Process } if (GetAsyncKeyState(MOUSEEVENTF_MOVE) < 0) { if (!is_mouse_down) { is_mouse_down = true; if (gun != nullptr) gun_index = 0; } if (gun != nullptr && gun_index != gun->len) { mouse_event(MOUSEEVENTF_MOVE, long(gun->pattner[gun_index][0] * K), long(gun->pattner[gun_index][1] * K), 0, 0); ++gun_index; Sleep(gun_delay); continue; } } else is_mouse_down = false; if (_kbhit())//Checks if there is currently keyboard input and returns a non-zero value if there is, or 0 otherwise { int ch = _getch(); if (ch == 0x32 || ch == 0x33 || ch == 0x34) { ch = _getch();//It waits for input and pauses the program } if (ch != 0x31) { while (true) { if (_kbhit()) { int ch = _getch(); if (ch == 0x31) break; } } } fflush(stdin);//Clear the input buffer } Sleep(150); } return 0; } 7 Comments
7VK
I was kind of confused by this code, when I put it in my source, several numbers appeared on the console
Junjie Zhu
This is just a sample, and you can't copy it directly into your program. My program outputs a number every 0.5 seconds to see if the program is running, and if you press (0x32) (0x33) (0x34) my program will pause the output of numbers and continue to output numbers when you press (0x31). This place( cout << i << endl;) outputs numbers, and you can put the parts of your program that need to be paused here.
7VK
look my code, how would you leave the code ?
Junjie Zhu
I modified the code in my answer for your reference.
7VK
I don't know what could be happening, but it's just pausing, it's not working again on the 0x31 key
|
getcharrequires that the user commit their input with a press of the enter key. The path of least resistance is probably to usegetchfrom the old DOS conio library. It's typically still supported under modern Windows systems.while (!AsyncKeyState(key1) && !AsyncKeyState(key2));to wait for any of key1 or key2getchar- so it should continue after the key is pressed. Disadvantage: Just polls and makes the processor warm until the key is pressed.