Search code examples
cwindowsconsole

How to disable echo in windows console?


How do I disable echo in a windows console C application?

I don't really want to capture characters with _getch (I still want Ctrl-C) to work. Besides _getch only seems to disable echo for cmd but not in cygwin.

There must be a way to redirect the pipe or modify the console settings.


Solution

  • Maybe SetConsoleMode (stolen from codeguru) :

    #include <iostream>
    #include <string>
    #include <windows.h>
    
    
    int main()
    {
        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
        DWORD mode = 0;
        GetConsoleMode(hStdin, &mode);
        SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
    
        std::string s;
        std::getline(std::cin, s);
    
        std::cout << s << "\n";
        return 0;
    }//main