How can I trap a keypress in C++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: How can I trap a keypress in C++?

  1. #1
    Join Date
    Apr 2002
    Mumbai, India

    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.


  2. #2
    Join Date
    Jan 2004
    Düsseldorf, Germany
    The following should theoretically work:
    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center