Getting console input without cin?

I am trying to create a small console program that will be mostly a console pon. So right now I have this:

int main()
{
    while(1)
    {
        clearScreen();
        restThread(100);
    }
    return 0;
}

The only input I need to poll is if the user presses the A or D key, since the screen has been cleared. I will also need to know when the key will be released. I am also trying to make this cross platform.

so really all i need is an if (keyWasDown ('a')) {} function.

thank

+5
source share
2 answers

, kbhit () getch (), <conio.h>. getchar, <stdio.h> <cstdio>.

, , getch getchar.

, , kbhit getch getchar .

, GMan, - ( , , ). ncurses.

+7

#include <stdio.h>
#include <conio.h>

int main()
{
    while(1)
    {
        clearScreen();

        if(kbhit())
        {
            int const ch = getch();
            switch(ch)
            {
            case 0x61: printf("A was pressed!\n"); break;
            case 0x64: printf("D was pressed!\n"); break;
            }
        }

        restThread(100);
    }

    return 0;
}

+3

All Articles