C++ 如何获取键盘按键值(或数字/代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24532048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:45:42  来源:igfitidea点击:

C++ How to get keyboard key press value (or number/code)

c++

提问by Mostafa Talebi

I want to see which key is pressed by user.

我想看看用户按下了哪个键。

I know there is cin()or getline(cin, var)but I don't want an input, I want to get the number of the key(index, or code it is called?).

我知道有cin()getline(cin, var)但我不想要输入,我想获取键的编号(索引或调用它的代码?)。

For instance, I want to get if the user has pressed F1or F10or Enter or Escape and then in return do something proper.

举例来说,我想,如果用户按下F1F10或Enter或Escape,然后在返回做一些正确的。

For instance:

例如:

if(user_has_pressed_escape)
{
 exit_the_console();
}

回答by odedsh

This is available via the OS own API. Different OSes have different APIs (for instance android does not have a F10 key at all).

这可以通过操作系统自己的 API 获得。不同的操作系统有不同的 API(例如 android 根本没有 F10 键)。

Often you will use a third party library to wrap the API so you can code independent from the OS. There are a lot of choices when it comes to a 3rd party library: SDL, QT, wxWidgets, GTKand many many more..

通常您会使用第三方库来包装 API,以便您可以独立于操作系统编写代码。当涉及到第三方库有很多选择:SDLQTwxWidgetsGTK和许多许多..

These libraries hide the interaction with the specific OS API from you and let you code once and run on many types of systems. But to understand how it works under the hood you might look at each OS documentation.

这些库对您隐藏了与特定 OS API 的交互,让您一次编码并在多种类型的系统上运行。但是要了解它的工作原理,您可以查看每个操作系统文档。

For instance on Windows GetKeyboardStateor PeekMessage

例如在 Windows GetKeyboardStatePeekMessage

On Linux X11: XQueryKeymapor via XPeekEvent

在 Linux X11 上:XQueryKeymap或通过XPeekEvent

回答by Dakorn

You can easily get event of ESC key down using ASCII table https://en.wikipedia.org/wiki/ASCII

您可以使用 ASCII 表https://en.wikipedia.org/wiki/ASCII轻松获取按下 ESC 键的事件

if (27 == getchar()) // 27 is for ESC
{
    //do something
}

Function keys (F1, F2, ...) depend on your OS.

功能键(F1、F2、...)取决于您的操作系统。

回答by Ben Key

According to the "INPUT VALUES" section of the "PDCurses User's Guide" found at http://pdcurses.sourceforge.net/doc/PDCurses.txt, the getch function can be used to detect such keys if keypad has been enabled.

根据http://pdcurses.sourceforge.net/doc/PDCurses.txt上的“PDCurses 用户指南”的“输入值”部分,如果键盘已启用,则 getch 函数可用于检测此类键。

Here are the relevant key codes.

以下是相关的关键代码。

KEY_F0      function keys; space for 64 keys is reserved
KEY_F(n)    (KEY_F0+(n))
KEY_EXIT    Exit key

The PDCurses library has the benefit of being cross platform.

PDCurses 库具有跨平台的优点。

回答by Andrey Tikhonov

C++ as a C ancestor is strongly system-independent, so there are no ways to do what you want using only the language itself, or even STL library. You will have to use different libraries on different platforms. Typically that will be only OS-dependent, because most operating system is actually looking for keyboard actions, like pressing, using interruption mechanism. So your program will have to interact with OS strongly.

C++ 作为 C 的祖先是高度独立于系统的,所以没有办法只使用语言本身,甚至 STL 库来做你想做的事。您将不得不在不同平台上使用不同的库。通常,这仅取决于操作系统,因为大多数操作系统实际上是在使用中断机制寻找键盘操作,例如按下。所以你的程序将不得不与操作系统强烈交互。

You should try avoid working with keyboard itself, if you do not know much about this topic.

如果您对此主题了解不多,则应尽量避免使用键盘本身。