Convert character to virtual key code

Jeremy picture Jeremy · Jun 30, 2010 · Viewed 17.1k times · Source

I have a string of values, and I want to simulate key press events in a window for each character.

I plan on sending WM_KEYDOWN, WM_CHAR, and WM_KEYUP events to the window (as that is what seems to happen whan a key is manually pressed).

Those messages require an int be sent in the wParam based on a table of virtual key codes. I can loop through the string and get each character, but how do I take that character and convert it to a value that corresponds to the virtual key code? Convert.ToInt32() does not work.

Answer

Andreas Magnusson picture Andreas Magnusson · Jul 1, 2010

VkKeyScanEx anyone? According to MSDN it:

"Translates a character to the corresponding virtual-key code and shift state."

(You could possibly also use VkKeyScan but beware that it has been superseded by VkKeyScanEx.)