Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get direct keyboard input in C++?

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

like image 455
Abdul Ghani Avatar asked Feb 17 '12 10:02

Abdul Ghani


2 Answers

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

like image 199
aib Avatar answered Oct 14 '22 05:10

aib


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.

like image 22
prathmesh.kallurkar Avatar answered Oct 14 '22 05:10

prathmesh.kallurkar