I am trying to determine if it is possible to take arrow keys and convert them to wide characters. I am using conio.h for its getch() function, I just like how it works compared to similar functions, and it has to be called twice to retrieve arrow keys.
Arrow keys when pressed return 0xE0 (-32) as the first character, and then {Left = 'K', Up = 'H', Right = 'M', Down = 'P'}
So I've been trying to find a way to merge the two characters into one. This is the closest thing I came up with. Function keys don't work with it though, it always returns the same values no matter the function key pressed. {F1-12 = 0, Arrows = 224} I Pulled out the trusty windows calculator and was able to determine that 224 is equivalent to -32 in binary. I just put it down to a byte and used the decimal system and went 100+124 and it was = -32.
So maybe somebody can help me figure out why the conversion is only considering the first character in the array. I have surely done something wrong. Enough talk, sorry for going on too long if that was the case and now here is the code.
#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;
}
}
}
}
Edit:
I found a way to combine the 2 bytes using a union. I didn't know what a union was at the time I posted this. The union allows me using the same memory space for two different data types. How it works is here - http://www.cplusplus.com/doc/tutorial/other_data_types/