C++ 中箭头键按下的平台独立检测

发布于 2024-09-19 01:22:41 字数 147 浏览 3 评论 0原文

在 C++ 控制台程序中,我找到了如何在 Windows 上检测和箭头键,并且我发现了很多与问题无关的其他内容(尽管我认为是很好的搜索词),但我想知道是否有一种独立于平台的方法来检测箭头键按下。一个不错的第二名是如何检测 unix 和 mac 中的箭头键按下。代码片段将不胜感激。

In a C++ console program, I've found how to detect and arrow key on Windows, and I've found a lot of other stuff that had nothing to do with the question (despite what I thought were good search terms), but I want to know if there is a platform-independent way to detect an arrow key press. A decent second place for this would be how to detect arrow key press in unix and mac. Code fragments would be appreciated.

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

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

发布评论

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

评论(3

淡水深流 2024-09-26 01:22:47

您可以使用 SDL2 来跨平台执行此操作。

示例代码:

    #include <SDL2/SDL.h>

    int main()
    {
        SDL_Event event;

        SDL_PollEvent(&event);

        if(event.type == SDL_KEYDOWN)
        {
            // Move centerpoint of rotation for one of the trees:
            switch(event.key.keysym.sym)
            {
                case SDLK_UP:
                    // do something
                    break;
                case SDLK_DOWN:
                    // do something
                    break;
                case SDLK_LEFT:
                    // do something
                    break;
                case SDLK_RIGHT:
                    // do something
                    break;
                case SDLK_ESCAPE:
                    // do something
                    return 0;
                default:
                    break;
            }
        }

        return 0;
    }

You can do this cross-platform by using SDL2.

Example code:

    #include <SDL2/SDL.h>

    int main()
    {
        SDL_Event event;

        SDL_PollEvent(&event);

        if(event.type == SDL_KEYDOWN)
        {
            // Move centerpoint of rotation for one of the trees:
            switch(event.key.keysym.sym)
            {
                case SDLK_UP:
                    // do something
                    break;
                case SDLK_DOWN:
                    // do something
                    break;
                case SDLK_LEFT:
                    // do something
                    break;
                case SDLK_RIGHT:
                    // do something
                    break;
                case SDLK_ESCAPE:
                    // do something
                    return 0;
                default:
                    break;
            }
        }

        return 0;
    }
毅然前行 2024-09-26 01:22:46

正如比利所说,没有标准的跨平台方法可以做到这一点。

就我个人而言,我将这个(面向游戏的)库用于所有输入,跨平台 win/linux/mac : http: //sourceforge.net/projects/wgois/

As Billy said, there is no standard cross-platform way to do it.

Personally I use this (game-oriented) library for all inputs, cross-platform win/linux/mac : http://sourceforge.net/projects/wgois/

〆凄凉。 2024-09-26 01:22:44

没有跨平台的方法可以做到这一点,因为它不是由 C 或 C++ 标准定义的(尽管可能有一些库可以在不同平台上编译时抽象出差异)。

我相信您在 POSIX 盒子上寻找的库是 curses,但我自己从未使用过它——我可能是错的。

请记住,控制台程序(即 gnome-terminalkonsolexterm)完全有可能垄断这些键的使用。功能。

There's no cross platform way to do it because it's not defined by either C or C++ standards (though there may be libraries which abstract away the differences when compiled on different platforms).

I believe the library you are looking for on POSIX boxes is curses, but I've never used it myself -- I could be wrong.

Keep in mind that it's entirely possible the console program (i.e. gnome-terminal or konsole or xterm) has monopolized the use of those keys for other functions.

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