2013-10-23 123 views
0

我想确定是否有可能将箭头键转换为宽字符。我为它的getch()函数使用了conio.h,我只是喜欢它与类似函数相比的工作原理,并且必须调用两次才能检索箭头键。箭头键为宽字符

箭头键按下时返回取0xE0(-32)作为第一个字符,然后{左= 'K',UP = '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/

回答

0

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

但是,只要将每个键都作为唯一的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取代short,但我想明白为什么要修复一些没有被破坏的东西。如果我决定在不同的程序的新解决方案中重新实现代码,我可能会这样做。

这也意味着a-Z & 0-9都可以作为它们的常规char值读取。