Detecting keypress in Visual C++

August 7, 2008

Jeez, I couldn’t believe how many people on the net have the same problem; detecting a key press from Visual C++ without echoing the same to the screen (ie: console).

I had this exact requirement today since I was trying to (re)write “Space Invaders” game to test the C++ console mode skills.

All I wanted to do was to detect the arrow keys and throw a message identifying the keys. But digging MSDN never helped me, it was overloaded with ’solutions’ :-(

After few experiments I figured the way to go forward is to use _kbhit from conio.h library. Remember conio.h is coming from ‘C’ not ‘C++’ so you got to use the ‘.h’ with it. Also note that usual keys return one character but special keys (function keys, arrow keys etc…) returns two characters. These are called ’scan codes’. Go here or here for more info; and if you are into assembly or electronics, here’s the text from the famous PC assembly language book, “Art of Assembly”. This note from MSDN explains the getch() function.

I wrote and tested this in Visual Studio 2005. Here’s the code:

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. char ch;
  10.  
  11. cout << "key press detector : use ‘z’, ‘x’ or arrow keys" << endl;
  12. cout << "press CTRL+C to exit. \n\n" << endl;
  13.  
  14. while(true) // this is the main loop
  15.         {
  16.                 if (_kbhit())
  17.                 {
  18.                         // in case of a key is hit, do these
  19.                         ch = getch();
  20.                         switch(ch)
  21.                         {
  22.                         case ‘z’ :
  23.                                 cout << "you pressed z" << endl;
  24.                                 break;
  25.  
  26.                         case ‘x’ :
  27.                                 cout << "you pressed x" << endl;
  28.                                 break;
  29.  
  30.                         case \0H’ :
  31.                                 cout << "you pressed up arrow key" << endl;
  32.                                 break;
  33.  
  34.                         case \0P’ :
  35.                                 cout << "you pressed down arrow key" << endl;
  36.                                 break;
  37.  
  38.                         case \0M’ :
  39.                                 cout << "you pressed right arrow key" << endl;
  40.                                 break;
  41.  
  42.                         case \0K’ :
  43.                                 cout << "you pressed left arrow key" << endl;
  44.                                 break;
  45.                         }
  46.                         //below line displays the key code
  47.                         cout << "key code=" << (int)ch << endl;
  48.                         //exit (1); // in this example, it is "exit the program!"
  49.                 }
  50.         }
  51. }

Here’s the source code and EXE for testing.

Allegro, a cool game development kit for DOS etc…

And a good Allegro example site

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • BlinkList
  • Furl
  • Reddit
  • Technorati
  • Fleck
  • YahooMyWeb
  • Netscape
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
Rate this article (164 views)
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3 out of 5)
Loading ... Loading ...
Print This Post Print This Post   Email This Post Email This Post

2 Responses to “Detecting keypress in Visual C++”

  1. AlexFM

    on August 7th, 2008 11:55 am

    Azmeer, you save my day ! a very big thank you…..
    This is exactly what I was looking for.

  2. RanjitW

    on August 12th, 2008 6:52 am

    Hi, thanks for the code. Do you need to use _kbhit at all? since you use getch() anyway?

Got something to say?