filecomplete/key_value.cxx

48 lines
1.0 KiB
C++

#include <iostream>
#include <termios.h>
#include <unistd.h>
// 设置终端为原始模式
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<int>(ch) << std::endl;
// 按 'q' 键退出程序
if (ch == 'q') {
break;
}
}
}
// 恢复终端模式
setRawMode(false);
return 0;
}