Can all keys be represented as a single char in C ++?

I searched around and I cannot find a way to present the arrow keys or escape key as a single char in C ++. Is it possible? I would expect it to be like \ t or \ n for a tab and a new line, respectively. Whenever I search for escaped characters, there is only a list of five or six known characters.

+4
source share
4 answers

The short answer is no.

The long answer is that in the standard ANSI character set there are several control characters (from decimal to decimal to 31, inclusive), including control codes for line feed, carriage return, end of file, and so on. Some of them are usually interpreted as arrows and an evacuation key, but only for compatibility with terminals.

Standard PC keyboards send a 2- or 3-byte control code that represents the key pressed, what state it is in, which controls the / Alt / Shift key, and several other things. You will need to find the β€œkey codes” to see how to handle them. Their processing differs between the operating systems and the underlying libraries you use, and their meaning varies depending on the keyboard configuration set by the operating system (which may contain characters not found in the ANSI character set).

+11
source

Impossible; a keyboard built for some languages ​​has characters that cannot be represented in char, and anyway, how do you represent control-option-command-shift-F11 in char?

Keyboards send scancodes, which are either a kind of event in the GUI system or a short string of bytes that represent the key. Which codes depend on your system, but on most terminal systems ncurses knows how to deal with them.

+1
source

char variables are usually elements in an ASCII table.
http://www.asciitable.com/
there is also an ascii man on unix. If you need the arrow keys, you'll need a more direct way to access the keyboard. arrow keys are translated in sequence of characters before hitting stdio. If oyu wants direct access to the keyboard, consider the GUI library, sdl, direct input, to name a few.

0
source

There are no escape characters for arrow keys. They are represented as Keycodes, afaik. I suggest using a higher level input library to detect keystrokes. If you want to do this from scratch, the approach may differ depending on the specific platform for which you are programming.

Anyway, in the days of TURBO C, I used -

//Wait for keypress //store input in ch //see if the ASCII code matches the code for one of the arrow keys 
0
source

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


All Articles