C中的连续键盘输入 [英] Continuous keyboard input in C

查看:116
本文介绍了C中的连续键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C语言中创建一个控制台应用程序。这是一个游戏,角色掉下来,用户必须按键盘上的特定键。我不知道如何在不暂停掉落字符的情况下检测用户按下了哪个键。当我使用scanf时,程序将等待输入,并且一切都会暂停。
请尽快帮助我!

I am creating a console application in C. This is a game in which characters are falling down and user has to press that specific key on the keyboard. I don't know how to detect which key is pressed by the user without pausing the falling characters. When I use scanf the Program waits for input and everything pauses. Please help me soon!

推荐答案

有一个名为 kbhit()或<$ c的函数$ c> _kbhit 它在< conio.h> 库中,它返回 true false 取决于是否按下了键。因此,您可以执行以下操作:

There is a function called kbhit() or _kbhit it is in the <conio.h> library it returns true or false depending whether a key was hit. So you can go with something like this:

while (1){
    if ( _kbhit() )
        key_code = _getch();
        // do stuff depending on key_code
    else 
        continue;

也可以使用 getch() _getch 直接从控制台而不是从缓冲区读取字符。您可以阅读有关 conio.h 函数的更多信息在这里它们对于您想做的事情可能非常有用。

Also use getch() or _getch which reads a character directly from the console and not from the buffer. You can read more about conio.h functions here they might be very useful for what you want to do.

注意: conio.h 不是标准库,实现因编译器而异。

Note: conio.h is not a standard library and implementations may vary from compiler to compiler.

这篇关于C中的连续键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆