How to get direct keyboard input in C++?

26,274

Solution 1

Have you considered using a console UI library such as ncurses?

Solution 2

In Windows, you can use the generic kbhit() function. This function returns true/false depending on whether there is a keyboard hit or not. You can then use the getch() function to read what is present on the buffer.

while(!kbhit()); // wait for input
c=getch();       // read input

You can also look at the scan codes. conio.h contains the required signatures.

Share:
26,274
Abdul Ghani
Author by

Abdul Ghani

Updated on July 09, 2022

Comments

  • Abdul Ghani
    Abdul Ghani almost 2 years

    I'm currently writing a game in C++ in windows. Everything is going great so far, but my menu looks like this:

    1.Go North

    2.Go South

    3.Go East

    4.Go North

    5.Inventory

    6.Exit

    Insert choice -

    It works fine, but I have been using that sort of thing for a while and would rather one that you can navigate with the up and down arrows. How would I go about doing this?

    Regards in advance