Tag: conio

kbhit()双循环不能正常工作

只是为了好玩,我尝试用循环打印kbhit() ,以便按下按键后的程序无限打印该行,直到再次按下键盘。 它编译得很好,在运行时,只是给出了空白屏幕。 没有打印。 但是在单键按下结束程序。 虽然控制台没有关闭。 #include #include int main() { while(1) { if(kbhit()) { while(1) { if(kbhit()) { goto out; } printf(“Print Ed Infinitum Until Key Press”); } } } out: return 0; } 我该如何解决这个问题?

函数参数和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’。 这是为什么会发生的?

clrscr()不工作,getch()工作。 为什么?

我正在制作一个小型C程序,它要求输入密钥并在switch语句中执行一些代码。 #include #include int main(int argc, char const *argv[]){ /* code */ printf(“Hello, press a, b or c to continue”); char key = getch(); switch (key){ case ‘a’: clrscr(); //some code break; case ‘b’: //many lines of code break; case ‘c’: clrscr(); //many lines of code break; default: printf(“Ok saliendo\n”); break; } printf(“bye”); } getch()工作正常,但clrscr()不是,即使我包含 […]

如何在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!!!!!!!!!! […]