2

I need to inject text into directinput applications. I'm using SendInput, but using virtual key codes doesn't work with directinput, so I need to use Unicode. Now, the keys I wan't to inject are registered on a file as virtual key codes, and i need to convert them to Unicode in real time.

return1 = ToUnicode(vk, MapVirtualKey(vk, MAPVK_VK_TO_VSC), NULL, buffer, 1, 0);

This is my code string. vk is the virtual key in a int, and buffer is a wchar_t array large 1 unit. I couldn't use just a simple wchar_t because it didn't work with ToUnicode. However, this function just returns 0 everytime, thus meaning the function couldn't translate the key. For the record, i'm using standard keys, such as "wasd", no special characters. If anyone can make out why this happens, I would really like some help.

Thanks for the consideration!

EDIT: also, would it be convenient to just fight my laziness and write some old switch and break to convert the values? eg.

wchar_t unicode;
switch (vk)
case '0x30':
    unicode = '48';
    //ecc...
Alex
  • 45
  • 1
  • 7
  • It would help if you gave an exact case of a failure...including the actual values going in. For instance say what `vk` is, and the result of `MapVirtualVk(vk, 0)`. Also, the second parameter to MapVirtualKey has constants defined for it...so `MAPVK_VK_TO_VSC` is clearer than just saying 0. – HostileFork says dont trust SE Aug 06 '14 at 01:28
  • Editing the question, anyway, as i wrote, this happens with every key. The result of *MapVirtualVk(vk, 0)*, as I think it should be, depend on the key. for example "3" gives 44, "a" gives 3030. – Alex Aug 06 '14 at 01:31

1 Answers1

1

It says that the "keys" array is optional, but unless I pass in a keys I get a zero result too. Maybe they just mean you have the option of having it work or not?

std::vector<BYTE> keys(256, 0);
int sc = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
return1 = ToUnicode(vk, sc, keys.data(), buffer, 1, 0);

So try that (or &keys[0] instead of keys.data() if you're not using C++11).

  • Thanks! Now ToUnicode returns the right values, even though SendInput with unicode flag on, wVk = 0 and Input.ki.wScan=buffer[0] still prints nothing :( – Alex Aug 06 '14 at 02:13
  • One little answer, if you know it thanks, if not thanks anyway. Can i just put in wScan the codes resulting from ToUnicode (eg. for "a", 97), or do I have to process them someway? – Alex Aug 06 '14 at 02:33
  • @Alex Faking input is a dodgy art in the first place, and I don't know much about DirectInput. So it really is best to open a new question, and give very precise test cases that don't work--so other people can reproduce it. – HostileFork says dont trust SE Aug 06 '14 at 02:40
  • Yes,already opened another question, and already solved the problem 10 minutes after. I didn't have to use Unicode, just hardware scan codes. Thanks Anyway! – Alex Aug 06 '14 at 08:33