ไม่มีโซลูชันแบบพกพาสำหรับการทำเช่นนี้ บน windows คุณสามารถใช้ฟังก์ชัน getch() จากไลบรารี conio(Console I/O) เพื่อกดตัวอักษร
ตัวอย่าง
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
char c;
while(1){ // infinite loop
c = getch();
cout << c;
}
} สิ่งนี้จะส่งออกอักขระที่คุณป้อนไปยังเทอร์มินัล โปรดทราบว่าการดำเนินการนี้จะใช้ได้บน windows เท่านั้น เนื่องจากไลบรารี conio มีอยู่ใน 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;
}
}