Detecting keypress

This is a discussion on Detecting keypress within the C Programming forums, part of the General Programming Boards category; I just remembered an old problem I once had. I once needed to detect a keypress for a game (e.g. ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Malta
    Posts
    9

    Detecting keypress

    I just remembered an old problem I once had. I once needed to detect a keypress for a game (e.g. user presses C and program takes the appropriate action without waiting for him to press ENTER). At the time I used the conio.h library's getch() function.

    Later I found out that this was not standard (not ANSI?) and not very portable.

    So is there a good way of detecting a keypress in C? It's just a matter of curiosity at this point.

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    In Windows, there's GetAsyncKeyState, but I don't know of any standard C or C++ instruction.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Nothing standard or truly portable, it's all OS specific.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    As mentioned before. It is OS specfic. There is no protable way of doing this. There is a good FAQ on this topic. Which explains solution on to main OS platforms.

    Go through this FAQ

    ssharish2005

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Malta
    Posts
    9
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (MFC, Visual C++) Keypress Detection in a dialog...
    By guitarist809 in forum Windows Programming
    Replies: 4
    Last Post: 08-31-2008, 02:13 PM
  2. Detecting empty file situation.
    By xIcyx in forum C Programming
    Replies: 9
    Last Post: 06-18-2008, 11:37 PM
  3. How do I NOT wait for a keypress?
    By misplaced in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2005, 05:10 PM
  4. Detecting a KeyPress
    By fuh in forum C++ Programming
    Replies: 12
    Last Post: 06-16-2003, 07:44 PM
  5. detecting keypress
    By lithium in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2003, 01:10 AM

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