Please a real simple Visual Studio C and C++ prof solution to avoid conio.h in W10 and input from console without waiting/return?

Jan van der Zanden 21 Reputation points
2021-09-14T12:17:59.63+00:00

I can nowhere find a simple (!), no tweaking, as much as possible standard C / C++ implementation in Windows Visual Studio of the old getkey() function that does not wait, but immediately responses with either the read character or a NILL/EOF code. Something like this [working under conio]:

if (_kbhit())
    { 
        ch = _getch();

        return(ch);
    }
 else
    {
        return(Nill); // or EOF or something like that
    }
Developer technologies C++
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2021-09-16T13:26:29.683+00:00

    The idea of the following sample is to use a wait function to see if there is console input. If there is input then the code looks for a key up record and returns the character value. Input records in the buffer that are not KEY_EVENT records are ignored. If the buffer is empty (or only contains events that are not KEY_EVENT records) the code returns EOF.

    int GetChNoWait()
    {
        INPUT_RECORD rec;
        HANDLE hInput = NULL;
    
        hInput = GetStdHandle(STD_INPUT_HANDLE);
        while (WaitForSingleObject(hInput, 0) == WAIT_OBJECT_0)
        {
            DWORD dwRead;
            ReadConsoleInput(hInput, &rec, 1, &dwRead);
            if (rec.EventType != KEY_EVENT)
                continue;
            else if(rec.Event.KeyEvent.bKeyDown)
                continue;
            else
            {
            #ifdef UNICODE
                return (int) rec.Event.KeyEvent.uChar.UnicodeChar;
            #else
                return (int) rec.Event.KeyEvent.uChar.AsciiChar;
            #endif
            }
        }
    
        return EOF;
    }
    

3 additional answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-09-14T16:07:06.32+00:00

    Hello @Jan van der Zanden

    Straight to the point, have you tried:

    fflush(stdin);  
    getchar();  
    

    Hope this helps,
    Best regards,


  2. Castorix31 90,521 Reputation points
    2021-09-15T20:38:17.15+00:00

    I have this MS function in archives (I just tested, it does the same thing as _getch()) :

    WCHAR myGetchar(void)
    {
        HANDLE hStdIn; /* standard input */
        DWORD dwInputMode; /* to save the input mode */
        BOOL bSuccess;
        WCHAR chBuf; /* buffer to read into */
        DWORD dwRead;
        /* get the standard input handle to read from. There is only one */
        /* instance of standard input per process at any given time */
        hStdIn = GetStdHandle(STD_INPUT_HANDLE);
        /* save the console mode */
        bSuccess = GetConsoleMode(hStdIn, &dwInputMode);
        /* disable line input. Echo input must be disabled when disabling */
        /* line input */
        bSuccess = SetConsoleMode(hStdIn, dwInputMode & ~ENABLE_LINE_INPUT & ~ENABLE_ECHO_INPUT);
        /* read a character from the console input */
        bSuccess = ReadFile (hStdIn, &chBuf, sizeof(chBuf), &dwRead, NULL);
        /* restore the original console input mode */
        bSuccess = SetConsoleMode(hStdIn, dwInputMode);
        return(chBuf);
    }
    

  3. Jan van der Zanden 1 Reputation point
    2021-09-16T13:28:26.777+00:00

    I will try both answers. Thanks!!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.