CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2002
    Location
    Mumbai, India
    Posts
    106

    How can I trap a keypress in C++?

    Hello everyone,
    I would be really grateful is someone could tell me how to trap a keypress in C++?I want to do a particular thing unless the user presses the enter key(or any key for that matter)..On detecting the keypress I want to move on and do something else..I tried the getch() method..But on doing that,it waits everytime for a keypress..which is not what I want...What I want is that the execution should proceed till the user presses a particular key...Any help would be highly appreciated.

    Thanks.
    -Ekta

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401
    The following should theoretically work:
    Code:
    char c;
    while ( ! cin.readsome( &c, 1 ) ) {
      // Do what you want to do
    }
    However you will run across the problem of buffering, which means that the input buffer will not actually contain any data untill the user has pressed return.

    Getting an actual key press can be very OS dependant. And you haven't told us which OS and which environment (console app, some graphical toolkit) you use.

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

Featured