2013-04-01 6 views
4

while 루프 내에서 키 누르기를 확인하기 위해 windows.h을 사용하지 않는 코드 스 니펫을 가지고있는 사람이 있습니까? 기본적으로이 코드를 사용하려면 windows.h을 사용하지 않아도됩니다. Linux와 Windows에서 사용하고 싶습니다.C++ : 키를 누를 때까지 while 루프를 실행합니다. Esc?

#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; 
} 
+0

'의 S/GetAsyncKeyState (VK_ESCAPE)/GetAsyncKeyState (VK_ESCAPE) 0x8000'. – chris

+0

@chris 죄송합니다. 무슨 뜻인지 이해가 안됩니다. – pandoragami

+0

특정 키를 누르거나 키를 누르시겠습니까? –

답변

2

가장 적합한 방법은 #IFDEF를 사용하여 Windows 및 Linux에서 적절한 GetAsyncKeyState() 또는 이와 동등한 것을 선택하는 사용자 지정 "GetAsyncKeyState"함수를 만드는 것입니다.

원하는 결과를 얻는 다른 방법은 없지만 Cin 접근 방식에는 응용 프로그램에 집중해야한다는 등의 문제가 있습니다.

+0

내가 생각하기에 너무. 나는'#ifdef _WIN32 #endif #ifdef linux # endif'를 사용하는 것을 좋아하지 않는다고 생각합니다. – pandoragami

+1

또 다른 해결책은 이러한 함수의 구현을 implement_linux.cpp와 implement_windows.cpp의 두 파일로하고, 사용하는 시스템에 따라 이들 중 하나를 컴파일하고 링크하는 것입니다. #ifdef가 필요 없습니다! – Nbr44

2
char c; 
while (cin >> c) { 
... 
} 

ctrl-D은 상기 루프를 종료한다. char가 입력되는 한 계속됩니다.

+0

지금 Windows XP에 있습니다. 그것을 시도하고 그냥 달려 있습니다. 나는 그것의 정상을 추측한다. – pandoragami

+0

@lost_with_coding 예, 표준 C++의 모든 입력이 차단됩니다. 나는 그 행동을 바꾸기 위해 해킹에 대해 확신하지 못한다. – chris

+0

@chris 그래서'windows.h'가없는 창에서 이것을 할 방법이 없습니까? – pandoragami

4
#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; 
} 
+0

그래서'getch()'는 단지 입력이없는'EOF'를 반환할까요? 'getchar()'이 더 이식성이 있지 않습니까? – Baldrickk

0

// 가장 단순합니다.

#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; 
    } 
-1
//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

여러 질문에 동일한 답변을 게시하지 마십시오.동일한 정보가 실제로 두 가지 질문에 모두 응답하면 하나의 질문 (일반적으로 새로운 질문)은 다른 질문의 사본으로 닫아야합니다. 이것을 [중복으로 투표로 투표] (http://stackoverflow.com/help/privileges/close-questions)로 표시하거나, 평판이 충분하지 않은 경우 [플래그 올림] (http://stackoverflow.com/help/privileges/flag-posts)가 중복되었음을 나타냅니다. 그렇지 않으면 * this * 질문에 대한 답을 맞추고 같은 장소에 여러 답을 붙여 넣지 마십시오. –