I am writing a basic program in C using Windows API to read from standard input and display it flawlessly on the standard out. The whole program works great but as soon as I quit the program, there are some trailing characters left behind on a terminal input. Maybe I am a little bit too picky about it but for me, it is too frustrating.
The code is the following:
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
/*********************************************************/
/* ClearScreen function definition .... */
struct Keys {
unsigned int k1: 1;
unsigned int k2: 1;
unsigned int k3: 1;
unsigned int k4: 1;
};
void check_key(struct Keys* keys)
{
keys->k1 = 0; keys->k2 = 0; keys->k3 = 0; keys->k4 = 0;
if (GetKeyState('A') & 0x8000)
{
keys->k1 = 1;
}
if (GetKeyState('S') & 0x8000)
{
keys->k2 = 1;
}
if (GetKeyState('D') & 0x8000)
{
keys->k3 = 1;
}
if (GetKeyState('F') & 0x8000)
{
keys->k4 = 1;
}
}
void print_keys(struct Keys keys)
{
ClearScreen();
printf("K1 = %d | K2 = %d | K3 = %d | K4 = %d",
keys.k1, keys.k2, keys.k3, keys.k4);
printf("\n");
printf("P1 = 1");
}
int main ()
{
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return -1;
struct Keys keys = {0, 0, 0, 0};
while (1)
{
check_key(&keys);
print_keys(keys);
Sleep(100);
}
CloseHandle(hStdOut);
return 0;
}
Example:
Let's say I am running this program and input the following keystrokes sequence "zczxcvcvbxxvbc". After the execution of the program I am left with this on my terminal:
What I want to achieve is to have clear terminal input after usage, without those characters in place.
Is there any way to achieve this?