箭头键作为宽字符

Arrow Keys as Wide Chars

本文关键字:字符      更新时间:2023-10-16

我正在尝试确定是否可以使用箭头键并将它们转换为宽字符。我正在使用conio.h作为它的getch()函数,与类似函数相比,我只是喜欢它的工作方式,并且必须调用两次才能检索箭头键。

按下箭头键时返回0xE0 (-32) 作为第一个字符,然后 {左 = 'K',上 = 'H',右 = 'M',下 = 'P'}

所以我一直在尝试找到一种方法将两个角色合并为一个。这是我想到的最接近的东西。功能键不适用于它,无论按下功能键,它始终返回相同的值。{F1-12 = 0,箭头 = 224}我拿出可靠的窗口计算器,能够确定 224 相当于二进制中的 -32。我只是把它放到一个字节并使用十进制系统,然后去 100+124,它是 = -32。

所以也许有人可以帮助我弄清楚为什么转换只考虑数组中的第一个字符。我肯定做错了什么。说得够多了,如果是这样的话,很抱歉持续太久,现在这里是代码。

#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <wincon.h>
#include <conio.h>
#include <cwchar>
/**int main()
{
    int N;
    char C;
    wchar_t R;
    while(true)
    {
        while(!kbhit()){}
        //C = getch();
        //if((R == 0) || (R == 224))
        std::cout << R << std::endl;
        N = R;
        std::cout << R << " = " << N << std::endl;
    }
}*/

int main()
{
    int N = 0;
    char C[2];
    wchar_t R;
    mbstate_t mbst;
    while(true)
    {
        mbrlen(NULL,0,&mbst);
        memset(&mbst,0,sizeof(mbst));
        for(int i = 0; i < 2; ++i)
        {
            while(!kbhit()){}
            C[i] = getch();
            N = C[i];
            switch(N)
            {
                case 0:
                    break;
                case -32:
                    break;
                default:
                    //input needs to be converted
                    mbrtowc(&R,C,2,&mbst);
                    N = R;
                    std::cout << R << " = " << N << std::endl;
                    i = 3;
                    break;
            }
        }
    }
}

编辑:

我找到了一种使用联合组合 2 个字节的方法。在我发布这篇文章的时候,我不知道工会是什么。联合允许我对两种不同的数据类型使用相同的内存空间。它是如何工作的在这里 - http://www.cplusplus.com/doc/tutorial/other_data_types/

我能够解决自己的问题,但不是以我一直尝试的方式。如果有人想帮助弄清楚我做错了什么,那就太好了。

然而,就将每个键作为唯一的 2 字节变量读取而言,我能够解决。我发现了一个关于移动位的问题,并用它来组合我的两个字符。然后我发现数字没有正确组合,我发现这是因为我使用的是有符号字符而不是无符号字符。我不想使用无符号字符,所以我找到了使用联合的新解决方案。

这是我的工会解决方案。这很简单。

#include <iostream>
#include <conio.h> //getch() & kbhit()
#include "rndgen.h"
#define FN_ESCAPE 27
#define FN_UP_ARROW 18656
#define FN_DOWN_ARROW 20704
#define FN_LEFT_ARROW 19424
#define FN_RIGHT_ARROW 19936
                                                                    //This union allows for two 8-bit values to be read as one 16-bit value 
union wide_char
{
    short wide_C;
    char C[2];
};
                                                                    //Function waits for a key to be pressed
inline short key_wait()
{
    wchar_t R;
    wide_char user_input;
    user_input.wide_C = 0;
                                                    //Loop twice, or until code causes the loop to exit
                                                        //Two times are neccessary for function keys unfortunately
    for(int i = 0; i < 2; ++i)
    {
                                                //While there isn't a key pressed, loop doing nothing
        while(!kbhit()){}
                                                //Grab the next key from the buffer
                                                    //Since the loop is done, there must be at least one
        user_input.C[i] = getch();
        switch(user_input.C[i])
        {
            case 0:
            case -32:
                                                //The key pressed is a Function key because it matches one of these two cases
                                                    //This means getch() must be called twice
                                                    //Break switch, run the for loop again ++i
                break;
            default:
                R = user_input.wide_C;
                return R;
                break;
        }
    }
    return -1;
}

wide_C按此顺序返回 C[2] 的位 {C[1],C[0]} 这很棒,因为这意味着 F1-12 可以唯一读取(F1-12 的第一个字符返回为 0) 用 wchar_t 替换短裤可能会,但我想为什么要修复没有损坏的东西。当我决定在另一个程序的新解决方案中重新实现代码时,我可能会这样做。

这也意味着 a-Z 和 0-9 都可以被读取为它们的常规字符值。