Kevin Meier
Kevin Meier

Reputation: 2582

ASCII character from VK_Code

I have a small WIN32 C-Application in which i work with the KBDLLHOOKSTRUCT structure. This structure contains the VK-Code for a pressed key.

I try to convert this to an ASCII-Character. For this i use the Function MapVirtualKey, which works well.

The only problem is, that one VK-Code can stay for multiple chars. Example:

On my keyboard (Swiss-German) exists the key-char .. If i press Shift+. then it creates a :. The VK-Code is the same. Thats no problem, and i can also check if Shift is pressed or Caps Lock is activated.

My only problem is: How can i get the char ':'? I need a function like this:

GetKeyChar(vkCode, shift)

I need this to get the "normal" and the "shifted" value of the keyboard. Of course i could hardcode this, but i don't like to do it on this way.

Upvotes: 6

Views: 9380

Answers (4)

Green Su
Green Su

Reputation: 2348

short VkKeyScan(char ch) API has contained the shift information. It translate char to virtual-key code and shift state.

See this: Convert character to the corresponding virtual-key code

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 134125

The problem is that the KBDLLHOOKSTRUCT doesn't have all the information you need in order to do the translation. You get a message every time a key is pressed. So for Shift+X, you'll get an input message saying that the Shift key was pressed, and another message saying that the "X" key was pressed.

You need to call GetKeyboardState in order to get the state of the Shift, Alt, Ctrl, (and perhaps other) keys. Then call ToAsciiEx or ToUnicodeEx.

Upvotes: 4

Alex F
Alex F

Reputation: 43331

The functions you are looking for are: ToAscii, ToAsciiEx, ToUnicode, ToUnicodeEx.

Upvotes: 2

phihag
phihag

Reputation: 288298

You're looking for ToUnicode, which returns the unicode character generated by that keypress.

Upvotes: 2

Related Questions