C ++ How to get the keypress value of a keyboard key (or number / code)

I want to see which key is pressed by the user.

I know that there is cin()or getline(cin, var), but I do not need an input, I want to get the key number (index or code that it is called?).

For example, I want to get if the user presses F1either F10either Enter or Escape, and then do something appropriate in return.

For instance:

if(user_has_pressed_escape)
{
 exit_the_console();
}
+4
source share
4 answers

It is available through the native OS API. Different OSs have different APIs (for example, android does not have an F10 key at all).

, API, . , : SDL, QT, wxWidgets, GTK .

API- . , , .

, Windows GetKeyboardState PeekMessage

Linux X11: XQueryKeymap XPeekEvent

+4

ESC ASCII http://pl.wikipedia.org/wiki/ASCII

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

(F1, F2,...) .

+2

++ C -, , , STL. . , , , . , .

, .

0

According to the “INPUT VALUES” section of the “PDCurses User Guide” found at http://pdcurses.sourceforge.net/doc/PDCurses.txt , the getch function can be used to detect such keys if the keyboard is enabled.

Here are the corresponding 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 takes advantage of the cross platform.

0
source

Source: https://habr.com/ru/post/1546859/


All Articles