print chars as the keys are hit

Hello everyone.

I found this method TCHAR win_getch()(below) on a website which is used for printing the characters as the keys on keyboard are hit i-e without hitting the ENTER key. This method is used in a while loop like this

1
2
while ((c = win_getch()) != 13)
{}


I wanted to know why is the character compared to 13 i-e if((c = win_getch()) != 13) then do something

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * This function will simulate the getch() function from conio.h.
 * Note that this does not work for special keys like F1-F12, arrow keys,
 * even the ESC key does not seem to work with this function.
 * @return TCHAR read from input buffer.
 */
TCHAR win_getch() {
	DWORD mode;
	TCHAR theChar = 0;
	DWORD count;
	//get a handle to stdin
	HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
	//save the current input mode...
	GetConsoleMode(hStdIn,&mode);
	// Clear the mode, turn off ENABLE_ECHO_INPUT and ENABLE_LINE_INPUT
	// so that the output will not be echoed and will not pause until the end of
	// a line for input.
	SetConsoleMode(hStdIn,0);	
	// Read in 1 char from the input buffer.
	ReadConsole(hStdIn,&theChar,sizeof(TCHAR),&count,NULL);
	//restore the current input mode.
	SetConsoleMode(hStdIn, mode);		
	return theChar;
}
13 would be the integer value for a carriage return.
13 is the code for carriage return. Then you press Entter key in Windows the code equal to 13 is generated.

By the way this is a bad code

while ((c = win_getch()) != 13)
{}


because there is no symbolic name for magic value 13. If there would be a symbolic name you could understand what this means.
Last edited on
Thanks LowestOne and vlad from moscow

The "char" value for carriage return is cr i-e (cr) = 13 but it doesn't work, how can I make it work?
'\n'
ok Thanks
Topic archived. No new replies allowed.