#include #include #include // 设置终端为原始模式 void setRawMode(bool enable) { static struct termios oldt, newt; if (enable) { // 获取当前终端设置 tcgetattr(STDIN_FILENO, &oldt); newt = oldt; // 设置终端为原始模式 newt.c_lflag &= ~(ICANON | ECHO); // 关闭回显和缓冲输入 tcsetattr(STDIN_FILENO, TCSANOW, &newt); } else { // 恢复原有终端设置 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); } } int main() { std::cout << "Press any key to see its keycode (Press 'q' to quit)..." << std::endl; // 启用原始模式 setRawMode(true); while (true) { // 读取按键 char ch; if (read(STDIN_FILENO, &ch, 1) == 1) { std::cout << "Keycode: " << static_cast(ch) << std::endl; // 按 'q' 键退出程序 if (ch == 'q') { break; } } } // 恢复终端模式 setRawMode(false); return 0; }