Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non blocking getch()

Tags:

c++

getch

I'm trying to make Tetris game in standard console. I need non-blocking getch(), so the blocks can fall without pressing any key. It would be nice to have function that returns -1 if no key pressed, otherwise the key code.

like image 701
noisy cat Avatar asked Jul 13 '12 14:07

noisy cat


1 Answers

This is exactly what you wanted:

int getch_noblock() {
    if (_kbhit())
        return _getch();
    else
        return -1;
}

Basically kbhit() does the job of determining if a key is pressed.

Assumes Windows and Microsoft Visual C++.

like image 130
quantum Avatar answered Oct 21 '22 13:10

quantum