이 작업을 수행할 수 있는 이식 가능한 솔루션이 없습니다. Windows에서는 conio(Console I/O) 라이브러리의 getch() 함수를 사용하여 문자를 누를 수 있습니다.
예
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
char c;
while(1){ // infinite loop
c = getch();
cout << c;
}
} 터미널에 입력한 모든 문자를 출력합니다. conio 라이브러리가 Windows에만 존재하기 때문에 이것은 Windows에서만 작동합니다. UNIX에서는 시스템 원시 모드로 들어가 이를 달성할 수 있습니다.
예
#include<iostream>
#include<stdio.h>
int main() {
char c;
// Set the terminal to raw mode
system("stty raw");
while(1) {
c = getchar();
// terminate when "." is pressed
if(c == '.') {
system("stty cooked");
exit(0);
}
std::cout << c << " was pressed."<< std::endl;
}
}