CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Posts
    146

    print chars as keys are hit

    Hello everyone.

    I found this method
    Code:
    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

    Code:
    while ((c = win_getch()) != 13)
    {}
    I wanted to know why is the character compared to
    Code:
    13
    i-e
    Code:
    if((c = win_getch()) != 13) then do something
    Code:
    /**
     * 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;
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Arrow Re: print chars as keys are hit

    Quote Originally Posted by gulHK View Post
    I wanted to know why is the character compared to
    Code:
    13
    i-e
    Code:
    if((c = win_getch()) != 13) then do something
    http://en.wikipedia.org/wiki/Carriage_return
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Nov 2010
    Posts
    146

    Re: print chars as keys are hit

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured