And I know there's std::cin, but that requires the user to enter a string, then press ENTER.  Is there a way to simply get the next key that is pushed without needing to press ENTER to confirm
            Asked
            
        
        
            Active
            
        
            Viewed 3,866 times
        
    2
            
            
         
    
    
        Jesse Beder
        
- 33,081
- 21
- 109
- 146
3 Answers
10
            
            
        What you're looking for is related to manipulating the console, and is OS-dependent. If you're in a UNIX-based OS, check out the curses library, and in Windows, there are getch() and kbhit() functions from <conio.h>.
 
    
    
        Jesse Beder
        
- 33,081
- 21
- 109
- 146
1
            
            
        It looks like the most upvoted answer is a bit outdated.
The ncurses library (based on the mentioned curses library) is a portable implementation available for unix and linux based operating systems, windows and others.
It supports a wide variety of terminal interfaces.
0
            You can use
#include <conio.h>
and then catch char with cases such as this
char c;
if (_kbhit())
{
  c = getch();
  switch(c)
  {
  case ‘\0H’ :
  cout << "up arrow key!" << endl;
  break;
  }
}
Beware: I have not tried it... and remember to put the whole thing into a "while(true)" to test.
 
    
    
        Dervin Thunk
        
- 19,515
- 28
- 127
- 217
- 
                    2I assume thatand _kbhit() are windows/DOS specific? – camh May 24 '09 at 02:07
- 
                    Also, hoe does '\OH' translate into up arrow? (As in, what are the values of the down, left, and right arrows?) – May 24 '09 at 02:31
- 
                    @camh: yes, afaik @Kean64: if i remember correctly, there are constants like ARROW_KEY_UP, please try that. not in front of my devel computer... – Dervin Thunk May 24 '09 at 02:58
- 
                    1This is not standard C++ – bcsanches Nov 25 '11 at 17:08
 
    