Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Location
    My room
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ Key Press Check

    How would I go about making a program that closes or carries out a function when the user presses the appropriate (or any) key? Such as:

    Code:
    if(key_press) {
         // do this
    }

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    The answer is platform-dependent. If you can live with having to press return after the key you can use std::cin.get(), but if not you'll have to resort to a native API or compiler feature to get the key in real-time.

    getch() is a common non-portable way to grab a key, and if you're programming for Windows you just need to look for the appropriate notification in your event loop (WM_KEYDOWN?)

    edit: cin.get() and getch() are synchronous, the notification isn't... you'll probably need to spawn a thread to look for the keypress with the synchronous methods if it's important that the input not block other stuff.
    Last edited by ralph l mayo; 10-15-2008 at 08:06 AM.

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Google gives http://www.fedoraforum.org/forum/sho...age=1&t=147415, which may help you.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •