Better key detection?

This is a discussion on Better key detection? within the C++ Programming forums, part of the General Programming Boards category; I'm planning on making a super simple game in the console in c++. The game would have to be in ...

  1. #1
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51

    Question Better key detection?

    I'm planning on making a super simple game in the console in c++. The game would have to be in a game loop. How do I check for input without waiting for the user to press the enter key?

    (What I'm looking for is similar to inkey$ in Qbasic, if that helps anyone)
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    I use the getch() command from the conio.h header.

    Basically, you'd do something like this:

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    char key_press;
    
    int main() {
         while (1) {
         cout << "Please press a key. \n";
         
         key_press = getch();
    
         cout << "You pressed the " << key_press << " key. \n \n";
         }
    }
    Of course, this doesn't work if the user input if the key is any kind of function like an arrow key or the Enter key. I'm not sure how to handle that input.

    This is also case-sensitive so "K" returns a different value than "Shift-K."
    Last edited by SlyMaelstrom; 04-22-2005 at 08:46 PM.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    That's neat. Is there a way to do that in a windows version?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > Is there a way to do that in a windows version?
    Like I said in my other reply, it's just a waste of time if you don't tell us the OS/Compiler you're using. All that happens is lots of pointless guessing and "this works for me posts" and/or people asking you to be MORE SPECIFIC.

    Well you could read the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Gahh!.
    Sorry salem.

    But what sly said worked perfectly, thank you sly.

    Also, I added that information to my signature, so, hmph!
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21