简体   繁体   English

Linux c ++控制台获得密钥状态

[英]Linux c++ console get key state

I want to create command line game in Linux but I don't know to get the key state. 我想在Linux中创建命令行游戏,但我不知道获得关键状态。 I heard of getch() but that stops the program. 我听说过getch()但是停止了程序。

I spent a little while reading around. 我花了一点时间阅读。 Apparently, this is a hard thing to do without the help of a library. 显然,没有图书馆的帮助,这是一件很难的事情。 Many people recommended the library ncurses. 很多人推荐图书馆ncurses。 If you want to try to do it by yourself, you need to learn about switching terminal modes and crazy stuff like that. 如果你想尝试自己做,你需要了解切换终端模式和疯狂的东西。 This thread was very informative: Non-blocking keyboard read - C/C++ 这个帖子非常有用: 非阻塞键盘读取 - C / C ++

In this article, the author implements a cKeyboard class, which directly listens for events from /dev/event0 . 文章中,作者实现cKeyboard类,它直接监听来自事件/dev/event0 The class is then used as follows: 然后按如下方式使用该类:

#include "keyboard.h"

cKeyboard kb;

  ...

if (kb.getKeyState(KEY_UP)) {
    // do something
}

It works perfectly for me, but I had to change event0 to event4 . 它对我来说很完美,但我不得不将event0更改为event4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM