K
K
Kirill Karpik2020-02-01 03:43:12
Keyboard
Kirill Karpik, 2020-02-01 03:43:12

How to separate pressing arrows and pressing Russian letters?

Hello. The getch() command returns the ASCII number of the pressed key. Russian letters are numbered in the ranges 127-176 and 223-242, inclusive. The arrow code consists of two numbers, where the first is always 224, which also stands for the letter 'p' and as a result the following code will not work, because if the letter 'p' was entered, the program will wait for the next letter to be entered.

int ch = getch();
if (ch == 224) {
  int arrow = getch();
  switch (arrow) {
    ... // обработка стрелок
  }
}

Решить проблему можно проверив введен ли уже второй символ, но cin.get() и cin.peek() не работают, так как, как я понял, из буффера cin символ удаляется после использования getch(). Как это решить? Буду рад и решениям, реализованным не через getch(), но желательно никакого winapi.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Армянское Радио, 2020-02-01
@gbg Куратор тега C++

The arrows do indeed generate two codes, but the first code is still 0, not 224.

V
Victor Bomberow, 2020-02-04
@majstar_Zubr

You don't need getch, don't use it.
https://docs.microsoft.com/en-us/cpp/c-runtime-lib...
If you still want to use it, then you will have to make a wrapper yourself to handle double-byte characters.
But everything is already invented, try _getwchar
https://docs.microsoft.com/en-us/cpp/c-runtime-lib...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question