I have the following menu, which is supposed to update based on whether the user has typed the keys F1 or F2:
int main()
{
bool f1 = false;
bool f2 = false;
while (true)
{
std::cout << "[F1]: " << (f1 ? "ON" : "OFF") << std::endl;
std::cout << "[F2]: " << (f2 ? "ON" : "OFF") << std::endl;
std::cout << "[INS] to quit" << std::endl;
if (GetAsyncKeyState(VK_INSERT) & 0x1)
break;
if (GetAsyncKeyState(VK_F1) & 0x1)
f1 = !f1;
if (GetAsyncKeyState(VK_F2) & 0x1)
f2 = !f2;
Sleep(100);
cleanWindow();
}
return 0;
}
Now, I was using system("cls")
before and it was working "fine", but I've been told that I should rather use the Win32 API for cleaning out the console, and so I created cleanWindow()
as described by
My question is, how do I do fix this? How do I bring the output back to the top of the shell?
EDIT:
I also edited the cleanWindow()
function to write the sequences: \033[2J
and L"\033[H"
with WriteConsoleW()
, which works, but I still get the "blinking" effect just like with system("cls")
, which is something I was trying to avoid.
CodePudding user response:
You can use SetConsoleCursorPosition
to set the cursor location back to the top left after doing the clear.
There are also ANSI escape codes (similar to the one you use to clear the screen) that would allow you to reposition the cursor.
"\033[r;cH"
replacing r
with the row and c
the column to move to. They are 1-based, and default to the top left, so you can use "\033[1;1H"
or just "\033[H"