0

I am currently having trouble correctly retrieving some of the keys on a standard keyboard (specifically F keys, arrow keys etc). I am cycling through all 256 ASCII characters using GetAsyncKeyState() on each integer to see if it is pressed, and if so stores it as a char to use in a later SendMessage() function.

this is so that I can allow a user to map a particular key to an Xbox 360 controller button/analog item by using XInput and then send the key to the topmost window when that button/analog item is triggered.

I am aware that the problem lies with hexadecimal/decimal conversion from the virtual key code identified by the GetAsyncKeyState() function as, for example, when I press F4 (hex 73, dec 115) the char is assigned to "s" which in ASCII is the same hex/dec value.

Is Unicode the way forward for this? if so how would I implement this to retrieve the right values for the SendMessage() function?

Below is the loop for GetAsyncKeyState()

while (mKey == NULL)
{
    system("cls");
    cout << "Press Key" << endl;
    for (int i = 0; i < 256; i++)
    {
        if (GetAsyncKeyState((i) & SHRT_MAX))
        {
            mKey = i;
        }
    }
}

I am aware system("cls") is bad practice but this console application is only a temporary solution.

Any help you can give will be greatly appreciated :)

David G
  • 94,763
  • 41
  • 167
  • 253
  • You might as well use `GetKeyboardState` if you're looping through all of them. – chris Feb 21 '14 at 22:15
  • This is all rather the wrong way to go about it and Unicode certainly doesn't have anything to do with virtual key codes. You left no clue at all what kind of problem you are *really* trying to solve. – Hans Passant Feb 21 '14 at 22:23
  • Hans - What i'm trying to do is retrieve a keypress (any key on the keyboard) which i then store in a map with a DWORD identifier for the Xinput button, this then gets used later on when the user is using the gamepad to find the key associated with the DWORD (as well as analog items) the key is then sent to the target window via the SendMessage() function, which all works fine, it's just being able to pick up a keypress, store that keypress, then send that key to a window. – Alex Blacklock Feb 21 '14 at 22:32
  • @chris - i found an addition at the bottom of the MSDN page for GetKeyboardState(), here CheerfulWaffle says that GetKeyboardState() will not detect key down events unless it is coupled with a subsequent GetKeyState() for each key which would place me in the same situation, any idea how i could make this work without the GetKeyState() calls? link-> [link](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646299(v=vs.85).aspx) – Alex Blacklock Feb 21 '14 at 22:42

0 Answers0