想做个登陆器,一来,不用保存密码到本地防止他人使用,二来,也省去输入密码
输入QQ号就不说了,简单得很,主要是密码,用sendmessage或postmessage是绝对行不通的,只好模拟键盘了 用API SendInput
关键代码贴在这里,给自己备份
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private static string Lcase_s = "abcdefghijklmnopqrstuvwxyz`1234567890-=\\[];',./";
private static string Ucase_s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|{}:\"<>?";
private static short[] KeyboardConstaint = new short[] { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90
,0xC0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48
,0xBD,0xBB,0xDC,0xDB,0xDD,0xBA,0xDE,0xBC,0xBE,0xBF};
public static bool SendKeyCommand(string key,IntPtr hwnd)
{
for (int i = 0; i < key.Length; i++)
{
string ch = key.Substring(i,1);
int index = Lcase_s.IndexOf(ch);
//是否在正常下
if (index >= 0)
{
short mykey = KeyboardConstaint[index];
Thread.Sleep(1);
SetFocus(hwnd);
SendKeyDown(mykey);
SendKeyUp(mykey);
}
else
{
index = Ucase_s.IndexOf(ch);
if (index >= 0)
{
short mykey = KeyboardConstaint[index];
Thread.Sleep(1);
SetFocus(hwnd);
SendKeyDown(VK_SHIFT);
SendKeyDown(mykey);
SendKeyUp(mykey);
SendKeyUp(VK_SHIFT);
}
else
{
return false;
}
}
}
return true;
}
public static void SendKeyDown(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.time = GetTickCount();
if (SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
public static void SendKeyUp(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
input[0].ki.time = GetTickCount();
if (SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private static string Ucase_s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|{}:\"<>?";
private static short[] KeyboardConstaint = new short[] { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90
,0xC0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48
,0xBD,0xBB,0xDC,0xDB,0xDD,0xBA,0xDE,0xBC,0xBE,0xBF};
public static bool SendKeyCommand(string key,IntPtr hwnd)
{
for (int i = 0; i < key.Length; i++)
{
string ch = key.Substring(i,1);
int index = Lcase_s.IndexOf(ch);
//是否在正常下
if (index >= 0)
{
short mykey = KeyboardConstaint[index];
Thread.Sleep(1);
SetFocus(hwnd);
SendKeyDown(mykey);
SendKeyUp(mykey);
}
else
{
index = Ucase_s.IndexOf(ch);
if (index >= 0)
{
short mykey = KeyboardConstaint[index];
Thread.Sleep(1);
SetFocus(hwnd);
SendKeyDown(VK_SHIFT);
SendKeyDown(mykey);
SendKeyUp(mykey);
SendKeyUp(VK_SHIFT);
}
else
{
return false;
}
}
}
return true;
}
public static void SendKeyDown(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.time = GetTickCount();
if (SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
public static void SendKeyUp(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
input[0].ki.time = GetTickCount();
if (SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}