12

Does anyone have a snippet of code that doesn't use windows.h to check for a key press within a while loop. Basically this code but without having to use windows.h to do it. I want to use it on Linux and Windows.

#include <windows.h>
#include <iostream>

int main()
{
    bool exit = false;

    while(exit == false)
    {
        if (GetAsyncKeyState(VK_ESCAPE))
        {
            exit = true;
        }
        std::cout<<"press esc to exit! "<<std::endl;
    }

    std::cout<<"exited: "<<std::endl;

    return 0;
}
pandoragami
  • 5,387
  • 15
  • 68
  • 116
  • `s/GetAsyncKeyState(VK_ESCAPE)/GetAsyncKeyState(VK_ESCAPE) & 0x8000`. – chris Apr 01 '13 at 04:11
  • @chris I'm sorry I don't understand what you mean. – pandoragami Apr 01 '13 at 04:11
  • Do you want a specific key to be pressed or any key? – Aswin Murugesh Apr 01 '13 at 04:12
  • @Aswin Any key is fine. – pandoragami Apr 01 '13 at 04:12
  • It means you should replace the first with the second. It's specified that if the key is down, the MSB is on. Standard C++ doesn't have a portable solution for this that I know of. I suppose a multithreaded `cin.get()` kind of works, but I'm very iffy with doing that. – chris Apr 01 '13 at 04:13
  • I believe that this post might be duplicate of [this one][1]. [1]: http://stackoverflow.com/questions/421860/c-c-capture-characters-from-standard-input-without-waiting-for-enter-to-be-pr# – Kupto Apr 01 '13 at 04:31
  • @Kupto No because they didn't ask for this to be multi-platform. Check out the answer. Its similar but I wanted this to work on windows and linux. Believe me I searched all over google before asking this. – pandoragami Apr 01 '13 at 04:35

5 Answers5

8
#include <conio.h>
#include <iostream>

int main()
{
    char c;
    std::cout<<"press esc to exit! "<<std::endl;
    while(true)
    {
        c=getch();
        if (c==27)
          break;
    }

    std::cout<<"exited: "<<std::endl;

    return 0;
}
Nasir Mahmood
  • 1,445
  • 1
  • 13
  • 18
2

Your best bet is to create a custom "GetAsyncKeyState" function that will use #IFDEF for windows and linux to choose the appropriate GetAsyncKeyState() or equivalent.

No other way exists to achieve the desired result, the cin approach has its problems - such as the application must be in focus.

  • Thats what I thought too. I don't like using `#ifdef _WIN32 #endif #ifdef linux #endif` but theres no other way I guess. – pandoragami Apr 01 '13 at 04:24
  • 1
    Another solution would be to have the implementation of such a function in two files, implement_linux.cpp and implement_windows.cpp for example, and simply compile and link with one of them depending on the system you're using. No #ifdef needed ! – Nbr44 Apr 01 '13 at 04:26
1
char c;
while (cin >> c) {
...
}

ctrl-D terminates the above loop. It will continue so long as a char is entered.

gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
-1

//simplest.

    #include <iostream>
    #include <conio.h>

using namespace std;
    int main()
    {

        char ch;
        bool loop=false;

      while(loop==false)
       {
        cout<<"press escape to end loop"<<endl;
        ch=getch();
        if(ch==27)
        loop=true;
       }
        cout<<"loop terminated"<<endl;
        return 0;
    }
AmmAr
  • 1
  • 1
-2
//its not the best but it works


#include <vector>
#define WINVER 0x0500
#include <windows.h>
#include <conio.h>
#include <iostream>

int main()
{
char c;
std::cout<<"press esc to exit! "<<std::endl;
while(true)
{

std::cout<<"executing code! , if code stops press any key to     continue or esc to stop"<<std::endl;


 INPUT ip;

// Set up a generic keyboard event.aa
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;

int lol =   65; //a key
    // Press the "A" key
ip.ki.wVk = lol; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));

// Release the "A" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));

    c=getch();
    if (c==27)
      break;
  }

std::cout<<"exited: "<<std::endl;
return 0;
}
  • 1
    Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by [voting to close it as a duplicate](http://stackoverflow.com/help/privileges/close-questions) or, if you don't have enough reputation for that, [raise a flag](http://stackoverflow.com/help/privileges/flag-posts) to indicate that it's a duplicate. Otherwise, be sure you tailor your answer to *this* question and don't just paste the same answer in multiple places. – elixenide Jun 17 '16 at 19:07