Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way of getting the virtual key code from char in C++?

Tags:

c++

keycode

Is there any way of getting this? I'm using the INPUT class to to simulate key presses, and when wanting to press the key in a variable of type char, I need to use the following method:

INPUT ip;
ip.type = INPUT_KEYBOARD;

//a bunch of code, and flags...

ip.ki.wVk = toupper(foo);

...where foo is of type char, and user-defined.

This works fine, until the user inputs a character that's not a regular letter, which results in a completely different key being registered (which is understandable). That's why I'm asking if there's possibly a method that makes me able to get the hexadecimal key code from the variable's value?

I know that I can make a big switch(foo) case '1': //and so on... to replace the unsupported characters with their corresponding key code, but really, that would mean a lot of unnecessary code.

So, is there any way? Answer is highly appreciated, and if there aren't any, I'll go a head and solve it in a more complicated way. However, since this issue has occured for me several times by now, I figured that it might be good to know for reference.

like image 879
Max Avatar asked Nov 19 '13 18:11

Max


1 Answers

To turn my comment into an answer...

I think you want VkKeyScanEx(...). This will turn a TCHAR into a virtual keycode and a set of keyboard modifiers that you should be able to use with SendInput(...)

like image 90
clstrfsck Avatar answered Oct 05 '22 02:10

clstrfsck