Thread: Need help with Console Keyboard Input

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    Question Need help with Console Keyboard Input

    I'm still a bit new to programming, but I decided to write an RPG and after a good start I got stuck at one point. Can anyone please tell me how can I, using the console, get keyboard input??? I don't mean just a

    cin<<???; or cin.get .

    How can I make it so that if someone pesses the key (arrow up) the cursor in my menu will move up one position, of if someone presses M the menu will pop up, or if the arrow down is pressed the player character will move down one position. I know how to program the functions, menus, etc, I just don't know how to avoid the typing in of commands. I was told it can be easily done with OpenGL, or others, but I wanna stick to basics. I wanna make it so the computer will wait until you press the key, then execute the function, etc.

    Thanks for any feedback.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Check out kbhit() in conio.h. Just have something like:
    Code:
    char key;
    if( kbhit( ) )
    {
      key = getch();
      switch( key )
      {
        // Handle all your keys here
    
        // 27 is decimal representation of ESC key
        case 27:
          // Call a function or
          // Quit program!
          break;
      }
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    4
    Thanks ... where can I find the character map for the keyboard? How can I find out the numbers for each key? Experiment???

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Write a program that prints out the ASCII code of the button you press.
    Code:
    if(kbhit())
    {
       Key=getch();
       cout << (int)Key << endl;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    this might help i used it for my game
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  6. #6
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    red_baron, I thank you for that. I coded a function to display the values of the keys, I had about a quarter of the list you have. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Keyboard Input
    By CaliJoe in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2009, 09:51 AM
  2. Console application - input handling
    By Something in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2007, 02:19 PM
  3. Stealing Keyboard input
    By zort15 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 02:22 PM
  4. Linux: Send keyboard input to background process
    By xErath in forum Tech Board
    Replies: 2
    Last Post: 12-09-2004, 07:02 PM
  5. Replies: 2
    Last Post: 10-09-2002, 11:22 PM