keybd_event, special characters and keyboard layouts?
I'm developing a C# .net application that allows users to insert text and have the appliaction automatically type it. What it does is parse every character of this text and send every keystroke seperatly.
However, I am wondering if there's a way to actually know what the output will be. For example,
However, I am a little worried about special characters and keyboard layouts.
For reference, I found this page with the key codes: http://msdn.microsoft.com/en-us/library/ms927178.aspx
This page has the folloing lines:
VK_OE开发者_运维知识库M_5 = "\|" for US
VK_OEM_102 = "<>" or "\|" on RT 102-key keyboard
Now, if my text conains a '\', how do I know if I have to send VK_OEM_5 or VK_OEM_102?
Thanks!
Why don't you just send WM_CHAR
s to the target window? This works with most programs(usually only games have problems with it). That way you circumvent the keyboard layout problem completely.
I've succeeded using this code that sends every char you want (in my sample is '%'), considering the keyboard layout.
short nCode = VkKeyScan('%');
if ((nCode & 0x0100) == 0x0100)
keybd_event(VK_LSHIFT, 0, 0, 0);
if ((nCode & 0x0200) == 0x0200)
keybd_event(VK_LCONTROL, 0, 0, 0);
if ((nCode & 0x0400) == 0x0400)
keybd_event(VK_RMENU, 0, 0, 0);
keybd_event((byte)(nCode & 0x00FF), 0, 0, 0);
keybd_event((byte)(nCode & 0x00FF), 0, KEYEVENTF_KEYUP, 0);
if ((nCode & 0x0100) == 0x0100)
keybd_event(VK_LSHIFT, 0, KEYEVENTF_KEYUP, 0);
if ((nCode & 0x0200) == 0x0200)
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);
if ((nCode & 0x0400) == 0x0400)
keybd_event(VK_RMENU, 0, KEYEVENTF_KEYUP, 0);
精彩评论