无法获得箭头键的正确值(Unable to get the correct value for the arrow keys)

编程入门 行业动态 更新时间:2024-10-26 10:32:06
无法获得箭头键的正确值(Unable to get the correct value for the arrow keys)

这是我的代码:

#include <iostream> #include <conio.h> #include <vector> #include <string> using namespace std; int startMenu(vector<string> arr, int pos){ /* Keyboard up : char(72) down : char(80) left : char(75) right : char(77) */ char userChar; refresh: system("cls"); for (int i = 0; i < arr.size(); i++){ if (i == pos){ cout << "> " << arr[i] << endl; } else{ cout << " " << arr[i] << endl; } } userChar = _getch(); switch (userChar){ case 0: case 0xE0: break; case 72: case 75: --pos; break; case 80: case 77: ++pos; break; case 13: return pos; default: break; } cout << pos << endl; if (pos >= arr.size()){ pos = 0; } if (pos == -1){ pos = arr.size() - 1; cout << "arr.size() - 1 = " << arr.size() - 1 << endl; } goto refresh; } int main(){ vector<string> arr; arr.push_back("Jamie"); arr.push_back("Alex"); startMenu(arr, 0); }

当pos为0并且我向上/向左键时, pos应该更改为1(因为arr.size() - 1 ),但是pos被读为0(因为箭头键返回'special'值)。

当pos为0时,如何在按左/向上箭头键后读取-1而不是0?

Here is my code :

#include <iostream> #include <conio.h> #include <vector> #include <string> using namespace std; int startMenu(vector<string> arr, int pos){ /* Keyboard up : char(72) down : char(80) left : char(75) right : char(77) */ char userChar; refresh: system("cls"); for (int i = 0; i < arr.size(); i++){ if (i == pos){ cout << "> " << arr[i] << endl; } else{ cout << " " << arr[i] << endl; } } userChar = _getch(); switch (userChar){ case 0: case 0xE0: break; case 72: case 75: --pos; break; case 80: case 77: ++pos; break; case 13: return pos; default: break; } cout << pos << endl; if (pos >= arr.size()){ pos = 0; } if (pos == -1){ pos = arr.size() - 1; cout << "arr.size() - 1 = " << arr.size() - 1 << endl; } goto refresh; } int main(){ vector<string> arr; arr.push_back("Jamie"); arr.push_back("Alex"); startMenu(arr, 0); }

When pos is 0 and I pressed up / left key, pos should be changed to be 1 (because arr.size() - 1), however pos is read as 0 (because arrow keys return 'special' values).

How can I read -1 instead of 0 after pressing left / up arrow keys when pos is 0?

最满意答案

编译时,您可能在此行上看​​到了签名/未签名的不匹配警告:

if (pos >= arr.size())

警告告诉你的是表达式中的一个值,在这种情况下,arr.size()是无符号的,而另一个,pos是有符号的。 这意味着为了比较它们,pos将首先转换为无符号值。 这意味着代替-1将是一个非常大的值,当然大于菜单中的行数,比较将为true,pos将设置为0。

修复的最小变化是将行更改为:

if(pos >= static_cast<int>(arr.size()))

这会强制arr.size()与pos的类型相同,因此可以直接比较值并解决您的问题。 它也摆脱了警告。

您还应该看到一些其他警告,for循环中的另一个有符号/无符号不匹配,显示菜单以及从_getch()的int到char警告的转换,因为它返回一个int并且您将它存储在char中。 这两个程序都不会导致问题,但您应该始终检查并更正它们以避免出现问题。

另一个潜在的问题是丢弃扩展键值0或0xE0意味着您无法判断该键是否真的是箭头键或具有您正在检查的ASCII值的实际键。 例如,键入H也会从_getch()返回72。

我不会试着和你说话,但在我看来你应该避免它。

You probably saw a signed/unsigned mismatch warning on this line when you compiled:

if (pos >= arr.size())

What that warning is telling you is that one of the values in the expression, in this case arr.size() is unsigned and the other, pos is signed. This means that in order to compare them, pos will be converted to an unsigned value first. That means that instead of -1 it will be a very large value, certainly larger than the number of lines in the menu, the comparison will be true and pos will be set to 0.

The smallest change to fix that would be to change the line to this:

if(pos >= static_cast<int>(arr.size()))

This forces arr.size() to be the same type as pos so the values can be compared directly and solves your problem. It also gets rid of the warning.

You should see some other warnings as well, another signed/unsigned mismatch in the for loop that displays the menu and a conversion from int to char warning for _getch() since it returns an int and you are storing it in a char. Neither should cause a problem in this program, but you should always check and correct them to avoid issues.

Another potential issue is that discarding the extended key value, 0 or 0xE0 means you have no way to tell if the key is really an arrow key or the actual key with the ASCII value you are checking for. As an example, typing H would also return 72 from _getch().

I won't try and talk you out of goto, but in my opinion you should avoid it.

更多推荐

本文发布于:2023-07-26 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   箭头键   Unable   keys   arrow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!