Tag: getch

为什么getch()没有读取输入的最后一个字符?

我正在使用ncurses库在C中编写一个蛇游戏,其中屏幕每秒更新一次。 当玩游戏的人知道,如果用户输入各种键或长按键,则不应存储“缓冲”按键。 换句话说,如果我按住w (向上键)并且stdin接收到20 w s的序列,然后输入d (右键),我希望蛇立即向右移动,并忽略缓冲w s。 我试图使用ncurses函数getch()实现这一点,但由于某种原因,我实现了我刚才描述的不良效果; 即在考虑最后一个按键之前,首先存储和处理任何长按键,因为这个MWE将说明: #include #include #include int main(){ char c = ‘a’; initscr(); cbreak(); noecho(); for(;;){ fflush(stdin); timeout(500); c = getch(); sleep(1); printw(“%c”, c); } return 0; } 有没有什么办法可以修改这段代码,以便getch()忽略任何缓冲的文本? 之前的fflush()似乎没有帮助。

如何在Linux中使用c的`getch`function?

我在Linux mint中安装了ncurses库,但我仍然不能在c中使用getch函数。 我正在使用Linux mint 18.2。 这是我的计划: #include #include int main() { char k; printf(“how are you”); k = getch(); printf(“%c”,k); } 这是输出: ram@ram$ gcc-7 test.c -lcurses ram@ram$ ./a.out how are you ram@ram$ 它不会等我按任何键并快速终止。 我不想为Linux安装conio.h 。 如何在Linux中使用getch和getche函数? 请不要告诉我自己的function。 我还是个菜鸟。 或者必须有替代品。

如何使用Escape键在C中结束循环

我只想在C中结束do while循环,如下所示: #include #include main() { char exit; do{ printf(“PLEASE INSERT OPTION:”); exit = getchar(); }while(exit != ‘\027’); }//main 我认为这有点像这样。

的getch(); 不会暂停循环

我有这段代码,例如: while(true){ printf(“looping”); getch(); } 而不是在每个循环上等待用户输入,循环只在没有我的情况下继续; 印花 loop loop loop 直到我关闭程序。 有没有更好的方法来阅读单个字符? 我真正想做的就是让用户输入’y’或’n’

函数参数和getch()的奇怪行为

我在使用包含getch()的参数调用函数时遇到了一些奇怪的行为。 以下面的代码为例: _Bool IsKeyDown(char c) { if(!kbhit()) return 0; char ch1 = getch(); printf(“%c\n”, c); return 0; } /* * */ int main(int argc, char** argv) { while(1) { IsKeyDown(‘a’); IsKeyDown(‘b’); Sleep(100); } return (EXIT_SUCCESS); } 当使用此代码按下某个键时,无论如何,它将始终打印’a’,这是第一个函数的参数。 问题是,’a’不是被调用的第二个函数的参数,但仍然打印’a’而不是’b’。 这是为什么会发生的?

C中“不适当的ioctl for device”错误

我有一个getch()函数,我的导师给了我,它是从键盘输入而不点击’ENTER’。 但是,当我在Eclipse中的Ubuntu 12中运行它时,我收到以下错误: tcsetattr(): Inappropriate ioctl for device tcsetattr ICANON: Inappropriate ioctl for device 这是我的代码: #include #include #include char getch(); int main(int argc, const char* argv[]) { char c; do { c=getch(); printf(“%c”,c); } while(c!=’q’); return 0; } char getch() { char buf = 0; struct termios old = {0}; if (tcgetattr(0, &old) < 0) […]

Linux的kbhit()问题

while(ch != ‘q’) { printf(“looping\n”); sleep(1); if(kbhit()) { ch = readch(); printf(“you hit %c\n”,ch); } } 这段代码给了我一个阻塞getch()之类的function。 我试图使用此代码捕获向下箭头键。 补充:尝试捕获向上箭头的关键代码给我3个字符27,91和65.使用if / else我正在尝试模式匹配,但我只得到2个字符。 按下下一个键时捕获下一个。 我想使用getchar()捕获完整的单词,同时总是一直在寻找某些键(esc,del等)。

如何在C中监听用户输入时运行程序?

我正在尝试制作一个继续运行的游戏,直到按下一个键,然后它应该接受该键并对其执行某些操作然后按照正常情况继续运行。 我该怎么做呢? 我在MAC上,所以尽管我遇到了一个名为conio.h的windows库,它可以使用kbhit()和getch()来处理这个问题,我无法让它为我工作…… // // main.c // conioTesting // // #include #include “myconio_mac.h” int main(int argc, const char * argv[]) { int counter = 0; while (counter < 2) { if (kbhit()) { char key = getch(); printf("\n Key is %c \n", key); printf("Keyboard hit detected \n"); } else { printf("Nothing. \n"); } } printf("Passed!!!!!!!!!! […]

C在按键上退出无限循环

当按下某个键时,如何退出无限循环? 目前我正在使用getch,但它会尽快开始阻止我的循环,因为没有更多的输入要读取。

getch和putchar没有返回工作

我一直试图让getch在另一个程序中工作但没有成功。 所以我已经制作了最基本的程序,我可以使用getch ,就像我希望它在主程序中工作一样。 我已经研究过对noecho , cbreak , initscr和nodelay ,我也试过使用newscr()但没有成功。 我遇到的问题是,在我点击“输入”之前,字符不会被打印到屏幕上,因为它们应该在每个循环中被put屏幕上。 为什么会这样? 此外,光标不会返回到新行的屏幕左侧。 例如。 abc def ghi 我找到了答案,但又被难倒…… #include #include int main() { initscr();cbreak(); noecho();nodelay(stdscr,0); char c ; while((c=getch())!=EOF){ putchar(c);} return 0; }