gpt4 book ai didi

c++ - 在 C/C++ 中创建自定义 getch() 方法

转载 作者:行者123 更新时间:2023-11-30 20:35:47 26 4
gpt4 key购买 nike

我想创建自己的自定义方法,其工作方式与 getch() 方法完全相同。无论是在 C/C++ 中。

最佳答案

像这样:

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}

/* reads from keypress, echoes */
int getche(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}

,我从 here 得到的.

关于c++ - 在 C/C++ 中创建自定义 getch() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com