如何以实时方式接受键盘输入?

发布于 2024-10-06 17:17:12 字数 416 浏览 0 评论 0原文

我目前正在用 C++ 编写一个程序,该程序与经典的“蛇”程序非常相似,但有一些更改。我正在编写它,以便它根据 ctime 每秒更新屏幕五次,如果可以处理,可能会更新更多次。我想做的一件事是让程序在 .h 文件“currkeypressed”中有一个变量。该变量将保存用户当前按住的键,并且蛇头将朝该方向移动。蛇中的每个节点都是一个单独的对象,因此按下的按键只会影响头部。每次更新时,方向和位置数据将沿着蛇从一个节点传递到另一个节点。

我的问题是:如何让程序持续保存这些数据,而不是仅在按下按键或按一定时间间隔时使用 cin 进行更新?我知道我可以在某种 for 循环中接受箭头键的 ascii 值,但是如果按住该键,并且如果循环很长,则可能会错过按下按钮,这似乎不起作用。有人知道该怎么做吗?

编辑:我使用的是 Linux 操作系统,更准确地说是 Ubuntu

I am currently writing a program in c++ that is very similar to the classic "snake" program with a few changes. I am writing it so that it will update the screen five times a second, based on ctime, possibly more if it can be handled. One thing that I would like to do is make it so that the program has a variable in the .h file "currkeypressed." This variable will hold the key that the user is currently holding down and the snake head will go in that direction. Each node in the snake will be a separate object so the currkeypressed will only affect the head. Directional and locational data will be passed down the snake from node to node at each update.

My question is: how can I get the program to keep this data constantly, rather than updating only with a cin when the key is pressed or at a certain interval? I know that I can accept the ascii value for the arrow keys in some sort of for loop, but that doesn't seem to work if the key is held down and if the loop is very long it can miss a press. Does anybody know how to do this?

EDIT: I am using a Linux OS, Ubuntu to be more precise

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再见回来 2024-10-13 17:17:12

最简单的方法是使用 ncurse 库。

但是,如果您更喜欢“原始”Linux 编程,则可以使用 selectgetchar

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
      fd_set readfs;
      struct timeval timeout;
      int res, key;

      while (1) {
         /* .. do something else .. */

         FD_SET(0, &readfs);  /* 0 is STDIN */
         timeout.tv_usec = 10;  /* milliseconds */
         timeout.tv_sec  = 0;  /* seconds */
         res = select(maxfd, &readfs, NULL, NULL, &timeout);
         if (res) {
            /* key pressed */
            key = getchar();
            if (key == 'A')  { /* blar */ }
         }
      }
}

The simplest method is to use ncurse library.

However, if you prefer "raw" linux programming, you can use select and getchar:

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
      fd_set readfs;
      struct timeval timeout;
      int res, key;

      while (1) {
         /* .. do something else .. */

         FD_SET(0, &readfs);  /* 0 is STDIN */
         timeout.tv_usec = 10;  /* milliseconds */
         timeout.tv_sec  = 0;  /* seconds */
         res = select(maxfd, &readfs, NULL, NULL, &timeout);
         if (res) {
            /* key pressed */
            key = getchar();
            if (key == 'A')  { /* blar */ }
         }
      }
}
月隐月明月朦胧 2024-10-13 17:17:12

一旦您超越了简单的面向行或字符的输入,您就会了解用户输入设备的细节。每个操作系统都有一个特定的机制来通知程序诸如 keydown 和 keyup 之类的事件。

要获得答案,请添加您所处编程环境的标签:MSWindows、Linux、Android、MacOS 等。

Once you are looking beyond simple line oriented or character orient input, you are into the details of the user input device. Each operating system has a particular mechanism for notifying a program of events like keydown and keyup.

To get an answer, add a tag for which programming environment you are in: MSWindows, Linux, Android, MacOS, etc.

隔纱相望 2024-10-13 17:17:12

您可以使用库 conio.h 并在循环中使用函数 _getch() 来不受任何限制地获取实时输入。

#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    char n='a';//Just for initialization 
    while(n!='e') 
    {
        n=_getch();
    }
    return 0;
}

You can use the library conio.h and you can use the function _getch() in a loop to get live input without any restriction.

#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    char n='a';//Just for initialization 
    while(n!='e') 
    {
        n=_getch();
    }
    return 0;
}
情话已封尘 2024-10-13 17:17:12

最简单(虽然实际上不是那么简单)的方法是使用一个线程来捕获用户输入,然后使用某种事件驱动系统来警告蛇的头部,或者蛇可以轮询当前的键。

The simplest (although really not that simple) way would be to have a thread to capture the user input and then either have some kind of event driven system that can alert the head of the snake or the snake can poll for the current key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文