Results 1 to 8 of 8

Thread: Capture key press - console app

  1. #1

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Capture key press - console app

    Is there a simple way of capturing a key press within a console app.

    At the moment i'm doing this:

    cout << "press any key to continue";
    cin >> x;

    but that looks messy as they first have to press a key, which is displayed after press any key to continue, and they then have to press enter/return aswell.

    Cheers.

  2. #2
    jim mcnamara
    Guest
    If you want the "Press any key to continue" kind of thing, where
    the use just presses a key and the program goes on:

    _getch()

    Code:
             #include <conio.h>
             int ch;
             ch = _getch();

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    #include <stdlib.h>

    void main()
    {
    system("pause");
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Thanks you 2, I'm a bit disappointed i didn't think of the
    system("pause") method myself though.

  5. #5
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    On Visual C++, system("pause") does not work either.
    Baaaaaaaaah

  6. #6

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    I'm using VC++ and it works fine in my console app. Although i've #include <cstdlib> instead of <stdlib.h> if that makes a difference.

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I never tried "cstdlib.h". I think it'll work for me.
    Baaaaaaaaah

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C:
    Code:
    #include <stdlib.h>
    
    int main(void) {
        system("pause");
        return 0;
    }
    C++
    Code:
    #include <cstdlib>
    
    using namespace std;
    
    int main() {
        system("pause");
        return 0;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width