字符串转键盘码
原内容 http://www.cnblogs.com/ccqin/articles/2317832.html
整理了 键盘码
int KEYEVENTF_KEYUP = 2;
byte VK_SHIFT = 16;
byte VK_SPACE = 32;
/// <summary>
/// 模拟输入
/// </summary>
/// <param name="str"></param>
public void KeyPress(string str)
{
for (int i = 0; i < str.Length; i++)//每一个字符分别转成ASCII
{
char key = str[i];
if (key == ' ')
{
keybd_event(VK_SPACE, 0, 0, 0);
keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0);
}
else if (key >= '0' && key <= '9')
{
keybd_event((byte)VkKeyScanA(key), 0, 0, 0);
keybd_event((byte)VkKeyScanA(key), 0, KEYEVENTF_KEYUP, 0);
}
else if (key >= 'A' && key <= 'Z')
{
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)VkKeyScanA(key), 0, 0, 0);
keybd_event((byte)VkKeyScanA(key), 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
}
else if (key >= 'a' && key <= 'z')
{
keybd_event((byte)VkKeyScanA(key), 0, 0, 0);
keybd_event((byte)VkKeyScanA(key), 0, KEYEVENTF_KEYUP, 0);
}
else if (key == '\t')
{
keybd_event((byte)Keys.Tab, 0, 0, 0);
keybd_event((byte)Keys.Tab, 0, KEYEVENTF_KEYUP, 0);
}
else
{
switch (key)
{
#region keys
case '\t':
keybd_event((byte)Keys.Tab, 0, 0, 0);
keybd_event((byte)Keys.Tab, 0, KEYEVENTF_KEYUP, 0);
break;
case ',':
keybd_event((byte)188, 0, 0, 0);
keybd_event((byte)188, 0, KEYEVENTF_KEYUP, 0);
break;
case '<':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)188, 0, 0, 0);
keybd_event((byte)188, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '.':
keybd_event((byte)190, 0, 0, 0);
keybd_event((byte)190, 0, KEYEVENTF_KEYUP, 0);
break;
case '>':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)190, 0, 0, 0);
keybd_event((byte)190, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '/':
keybd_event((byte)191, 0, 0, 0);
keybd_event((byte)191, 0, KEYEVENTF_KEYUP, 0);
break;
case '?':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)191, 0, 0, 0);
keybd_event((byte)191, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '\\':
keybd_event((byte)220, 0, 0, 0);
keybd_event((byte)220, 0, KEYEVENTF_KEYUP, 0);
break;
case '|':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)220, 0, 0, 0);
keybd_event((byte)220, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case ';':
keybd_event((byte)186, 0, 0, 0);
keybd_event((byte)186, 0, KEYEVENTF_KEYUP, 0);
break;
case ':':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)186, 0, 0, 0);
keybd_event((byte)186, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '\'':
keybd_event((byte)222, 0, 0, 0);
keybd_event((byte)222, 0, KEYEVENTF_KEYUP, 0);
break;
case '"':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)222, 0, 0, 0);
keybd_event((byte)222, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '[':
keybd_event((byte)219, 0, 0, 0);
keybd_event((byte)219, 0, KEYEVENTF_KEYUP, 0);
break;
case '{':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)219, 0, 0, 0);
keybd_event((byte)219, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case ']':
keybd_event((byte)219, 0, 0, 0);
keybd_event((byte)219, 0, KEYEVENTF_KEYUP, 0);
break;
case ' ':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)221, 0, 0, 0);
keybd_event((byte)221, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '-':
keybd_event((byte)189, 0, 0, 0);
keybd_event((byte)189, 0, KEYEVENTF_KEYUP, 0);
break;
case '_':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)189, 0, 0, 0);
keybd_event((byte)189, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '=':
keybd_event((byte)187, 0, 0, 0);
keybd_event((byte)187, 0, KEYEVENTF_KEYUP, 0);
break;
case '+':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)187, 0, 0, 0);
keybd_event((byte)187, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '`':
keybd_event((byte)192, 0, 0, 0);
keybd_event((byte)192, 0, KEYEVENTF_KEYUP, 0);
break;
case '~':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)192, 0, 0, 0);
keybd_event((byte)192, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '!':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)49, 0, 0, 0);
keybd_event((byte)49, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '@':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)50, 0, 0, 0);
keybd_event((byte)50, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '#':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)51, 0, 0, 0);
keybd_event((byte)51, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '$':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)52, 0, 0, 0);
keybd_event((byte)52, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '%':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)53, 0, 0, 0);
keybd_event((byte)53, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '^':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)54, 0, 0, 0);
keybd_event((byte)54, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '&':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)55, 0, 0, 0);
keybd_event((byte)55, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '*':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)56, 0, 0, 0);
keybd_event((byte)56, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '(':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)57, 0, 0, 0);
keybd_event((byte)57, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case ')':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event((byte)48, 0, 0, 0);
keybd_event((byte)48, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
#endregion
}
}
}
}