/*===========================================================
*author : archerzhang
*mailto: archerzhang@163.com
*date:2008.10.16
===========================================================*/
#include "winable.h"
BOOL InputStrByKeyEvent(char *szinfo);
/* 下面两个为测试代码*/
DWORD WINAPI testThreadFun( LPVOID lpParameter );
DWORD WINAPI loginQQfun( LPVOID lpParameter );
void LoginQQ()
{
ShellExecute(NULL, "open", "D://Program Files//Tencent//QQ//QQ.exe", NULL, NULL, SW_SHOW);
HANDLE hd=CreateThread(NULL, 0, loginQQfun, NULL, 0, NULL);
}
void Test1(void)
{
static char str[]="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz`~1!2@3#4$5%6^7&8*9(0)-_=+//|[{]};:/'/",<.>/? a/t";
ShellExecute(NULL, "open", "Notepad.exe", NULL, NULL, SW_SHOW);
HANDLE hd=CreateThread(NULL, 0, testThreadFun, &str, 0, NULL);
}
DWORD WINAPI loginQQfun( LPVOID lpParameter )
{
int i=0;
HWND tmpWnd;
for (i=0; i<100; i++)
{
tmpWnd = ::FindWindow(NULL, "QQ用户登录");
::BringWindowToTop(tmpWnd);
if (tmpWnd)
{
InputStrByKeyEvent("/t");
Sleep(500);
InputStrByKeyEvent("12345678"); //QQ ID
InputStrByKeyEvent("/t");
Sleep(500);
InputStrByKeyEvent("your qq password");//QQ Password
InputStrByKeyEvent("/n");
break;
}
Sleep(500);
TRACE("~~~/n");
}
return 0;
}
DWORD WINAPI testThreadFun( LPVOID lpParameter )
{
char *pstr = (char *)lpParameter;
Sleep(1000);
InputStrByKeyEvent(pstr);
return 0;
}
//------------------------------------------------------------------------
struct C2VK_T
{
char ch;
BYTE VK;
BYTE bShift;
BYTE bCaps;
};
C2VK_T g_C2VKMap[]=
{
{'0', 48, 0, -1},
{'1', 49, 0, -1},
{'2', 50, 0, -1},
{'3', 51, 0, -1},
{'4', 52, 0, -1},
{'5', 53, 0, -1},
{'6', 54, 0, -1},
{'7', 55, 0, -1},
{'8', 56, 0, -1},
{'9', 57, 0,- 1},
{')', 48, 1, -1},
{'!', 49, 1, -1},
{'@', 50, 1, -1},
{'#', 51, 1, -1},
{'$', 52, 1, -1},
{'%', 53, 1, -1},
{'^', 54, 1, -1},
{'&', 55, 1, -1},
{'*', 56, 1, -1},
{'(', 57, 1, -1},
{'/t', 9, -1,-1},
{'`', 192, 0, -1},
{'~', 192, 1, -1},
{'-', 189, 0, -1},
{'_', 189, 1, -1},
{'=', 107, 0, -1},
{'+', 107, 1, -1},
{';', 186, 0, -1},
{':', 186, 1, -1},
{',', 188, 0, -1},
{'<', 188, 1, -1},
{'.', 190, 0, -1},
{'>', 190, 1, -1},
{'/'', 222, 0, -1},
{'/"', 222, 1, -1},
{'[', 219, 0, -1},
{'{', 219, 1, -1},
{']', 221, 0, -1},
{'}', 221, 1, -1},
{'/', 191, 0, -1},
{'?', 191, 1, -1},
{'//', 220, 0, -1},
{'|', 220, 1, -1},
{'A',65,0,1},
{'B',66,0,1},
{'C',67,0,1},
{'D',68,0,1},
{'E',69,0,1},
{'F',70,0,1},
{'G',71,0,1},
{'H',72,0,1},
{'I',73,0,1},
{'J',74,0,1},
{'K',75,0,1},
{'L',76,0,1},
{'M',77,0,1},
{'N',78,0,1},
{'O',79,0,1},
{'P',80,0,1},
{'Q',81,0,1},
{'R',82,0,1},
{'S',83,0,1},
{'T',84,0,1},
{'U',85,0,1},
{'V',86,0,1},
{'W',87,0,1},
{'X',88,0,1},
{'Y',89,0,1},
{'Z',90,0,1},
{'a',65,0,0},
{'b',66,0,0},
{'c',67,0,0},
{'d',68,0,0},
{'e',69,0,0},
{'f',70,0,0},
{'g',71,0,0},
{'h',72,0,0},
{'i',73,0,0},
{'j',74,0,0},
{'k',75,0,0},
{'l',76,0,0},
{'m',77,0,0},
{'n',78,0,0},
{'o',79,0,0},
{'p',80,0,0},
{'q',81,0,0},
{'r',82,0,0},
{'s',83,0,0},
{'t',84,0,0},
{'u',85,0,0},
{'v',86,0,0},
{'w',87,0,0},
{'x',88,0,0},
{'y',89,0,0},
{'z',90,0,0},
{' ', VK_SPACE, 0, 0},
{'/n', VK_RETURN, 0, 0},
{0}
};
BOOL SetShift(BOOL bShiftPress)
{
int bPressed;
short istate;
INPUT input={0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_SHIFT;
input.ki.dwFlags =0;
istate = GetKeyState(VK_SHIFT);
bPressed = ((istate == -128) || (istate == -127));
if (bShiftPress )
{
if (!bPressed)
{
input.ki.dwFlags =0;
SendInput(1, &input, sizeof(input));
}
}
if (!bShiftPress )
{
if (bPressed)
{
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
TRACE("result shift = %d/n/n", istate);
return bShiftPress;
}
BOOL SetCaps(BOOL bState)
{
short istate;
BOOL bPressed;
INPUT input={0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_CAPITAL;
input.ki.dwFlags =0;
istate = GetKeyState(VK_CAPITAL) ;
bPressed = ((istate == 1) || (istate == -127));
#if 1
if (bState)
{
if (!bPressed)
{
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
else
{
if (bPressed)
{
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
#else
if (bState != bPressed)
{
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
#endif
return TRUE;
}
BOOL ExecuteKeyEvent(C2VK_T *vkinfo)
{
INPUT input={0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = 0;
input.ki.dwFlags =0;
if ((vkinfo->bCaps == 0) || (vkinfo->bCaps == 1))
{
SetCaps(vkinfo->bCaps);
}
if ((vkinfo->bShift ==0 ) || (vkinfo->bShift == 1))
{
SetShift(vkinfo->bShift);
}
input.ki.wVk = vkinfo->VK;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
if ((vkinfo->bShift ==0) || (vkinfo->bShift == 1))
{
SetShift(FALSE);
}
return TRUE;
}
BOOL ch2VKInfo(char c, C2VK_T *vkinfo)
{
int i;
BOOL bFind= FALSE;
for (i=0; g_C2VKMap[i].ch; i++)
{
if (g_C2VKMap[i].ch == c)
{
memcpy(vkinfo, &(g_C2VKMap[i]), sizeof(C2VK_T));
bFind = TRUE;
break;
}
}
return bFind;
}
BOOL InputStrByKeyEvent(char *szinfo)
{
char *p;
C2VK_T vkinfo={0};
for (p=szinfo; *p; p++)
{
if (ch2VKInfo(*p, &vkinfo))
{
//Sleep(200);
ExecuteKeyEvent(&vkinfo);
}
else
{
return FALSE;
}
}
return TRUE;
}
*author : archerzhang
*mailto: archerzhang@163.com
*date:2008.10.16
===========================================================*/
#include "winable.h"
BOOL InputStrByKeyEvent(char *szinfo);
/* 下面两个为测试代码*/
DWORD WINAPI testThreadFun( LPVOID lpParameter );
DWORD WINAPI loginQQfun( LPVOID lpParameter );
void LoginQQ()
{
ShellExecute(NULL, "open", "D://Program Files//Tencent//QQ//QQ.exe", NULL, NULL, SW_SHOW);
HANDLE hd=CreateThread(NULL, 0, loginQQfun, NULL, 0, NULL);
}
void Test1(void)
{
static char str[]="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz`~1!2@3#4$5%6^7&8*9(0)-_=+//|[{]};:/'/",<.>/? a/t";
ShellExecute(NULL, "open", "Notepad.exe", NULL, NULL, SW_SHOW);
HANDLE hd=CreateThread(NULL, 0, testThreadFun, &str, 0, NULL);
}
DWORD WINAPI loginQQfun( LPVOID lpParameter )
{
int i=0;
HWND tmpWnd;
for (i=0; i<100; i++)
{
tmpWnd = ::FindWindow(NULL, "QQ用户登录");
::BringWindowToTop(tmpWnd);
if (tmpWnd)
{
InputStrByKeyEvent("/t");
Sleep(500);
InputStrByKeyEvent("12345678"); //QQ ID
InputStrByKeyEvent("/t");
Sleep(500);
InputStrByKeyEvent("your qq password");//QQ Password
InputStrByKeyEvent("/n");
break;
}
Sleep(500);
TRACE("~~~/n");
}
return 0;
}
DWORD WINAPI testThreadFun( LPVOID lpParameter )
{
char *pstr = (char *)lpParameter;
Sleep(1000);
InputStrByKeyEvent(pstr);
return 0;
}
//------------------------------------------------------------------------
struct C2VK_T
{
char ch;
BYTE VK;
BYTE bShift;
BYTE bCaps;
};
C2VK_T g_C2VKMap[]=
{
{'0', 48, 0, -1},
{'1', 49, 0, -1},
{'2', 50, 0, -1},
{'3', 51, 0, -1},
{'4', 52, 0, -1},
{'5', 53, 0, -1},
{'6', 54, 0, -1},
{'7', 55, 0, -1},
{'8', 56, 0, -1},
{'9', 57, 0,- 1},
{')', 48, 1, -1},
{'!', 49, 1, -1},
{'@', 50, 1, -1},
{'#', 51, 1, -1},
{'$', 52, 1, -1},
{'%', 53, 1, -1},
{'^', 54, 1, -1},
{'&', 55, 1, -1},
{'*', 56, 1, -1},
{'(', 57, 1, -1},
{'/t', 9, -1,-1},
{'`', 192, 0, -1},
{'~', 192, 1, -1},
{'-', 189, 0, -1},
{'_', 189, 1, -1},
{'=', 107, 0, -1},
{'+', 107, 1, -1},
{';', 186, 0, -1},
{':', 186, 1, -1},
{',', 188, 0, -1},
{'<', 188, 1, -1},
{'.', 190, 0, -1},
{'>', 190, 1, -1},
{'/'', 222, 0, -1},
{'/"', 222, 1, -1},
{'[', 219, 0, -1},
{'{', 219, 1, -1},
{']', 221, 0, -1},
{'}', 221, 1, -1},
{'/', 191, 0, -1},
{'?', 191, 1, -1},
{'//', 220, 0, -1},
{'|', 220, 1, -1},
{'A',65,0,1},
{'B',66,0,1},
{'C',67,0,1},
{'D',68,0,1},
{'E',69,0,1},
{'F',70,0,1},
{'G',71,0,1},
{'H',72,0,1},
{'I',73,0,1},
{'J',74,0,1},
{'K',75,0,1},
{'L',76,0,1},
{'M',77,0,1},
{'N',78,0,1},
{'O',79,0,1},
{'P',80,0,1},
{'Q',81,0,1},
{'R',82,0,1},
{'S',83,0,1},
{'T',84,0,1},
{'U',85,0,1},
{'V',86,0,1},
{'W',87,0,1},
{'X',88,0,1},
{'Y',89,0,1},
{'Z',90,0,1},
{'a',65,0,0},
{'b',66,0,0},
{'c',67,0,0},
{'d',68,0,0},
{'e',69,0,0},
{'f',70,0,0},
{'g',71,0,0},
{'h',72,0,0},
{'i',73,0,0},
{'j',74,0,0},
{'k',75,0,0},
{'l',76,0,0},
{'m',77,0,0},
{'n',78,0,0},
{'o',79,0,0},
{'p',80,0,0},
{'q',81,0,0},
{'r',82,0,0},
{'s',83,0,0},
{'t',84,0,0},
{'u',85,0,0},
{'v',86,0,0},
{'w',87,0,0},
{'x',88,0,0},
{'y',89,0,0},
{'z',90,0,0},
{' ', VK_SPACE, 0, 0},
{'/n', VK_RETURN, 0, 0},
{0}
};
BOOL SetShift(BOOL bShiftPress)
{
int bPressed;
short istate;
INPUT input={0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_SHIFT;
input.ki.dwFlags =0;
istate = GetKeyState(VK_SHIFT);
bPressed = ((istate == -128) || (istate == -127));
if (bShiftPress )
{
if (!bPressed)
{
input.ki.dwFlags =0;
SendInput(1, &input, sizeof(input));
}
}
if (!bShiftPress )
{
if (bPressed)
{
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
TRACE("result shift = %d/n/n", istate);
return bShiftPress;
}
BOOL SetCaps(BOOL bState)
{
short istate;
BOOL bPressed;
INPUT input={0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_CAPITAL;
input.ki.dwFlags =0;
istate = GetKeyState(VK_CAPITAL) ;
bPressed = ((istate == 1) || (istate == -127));
#if 1
if (bState)
{
if (!bPressed)
{
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
else
{
if (bPressed)
{
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
#else
if (bState != bPressed)
{
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
#endif
return TRUE;
}
BOOL ExecuteKeyEvent(C2VK_T *vkinfo)
{
INPUT input={0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = 0;
input.ki.dwFlags =0;
if ((vkinfo->bCaps == 0) || (vkinfo->bCaps == 1))
{
SetCaps(vkinfo->bCaps);
}
if ((vkinfo->bShift ==0 ) || (vkinfo->bShift == 1))
{
SetShift(vkinfo->bShift);
}
input.ki.wVk = vkinfo->VK;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
if ((vkinfo->bShift ==0) || (vkinfo->bShift == 1))
{
SetShift(FALSE);
}
return TRUE;
}
BOOL ch2VKInfo(char c, C2VK_T *vkinfo)
{
int i;
BOOL bFind= FALSE;
for (i=0; g_C2VKMap[i].ch; i++)
{
if (g_C2VKMap[i].ch == c)
{
memcpy(vkinfo, &(g_C2VKMap[i]), sizeof(C2VK_T));
bFind = TRUE;
break;
}
}
return bFind;
}
BOOL InputStrByKeyEvent(char *szinfo)
{
char *p;
C2VK_T vkinfo={0};
for (p=szinfo; *p; p++)
{
if (ch2VKInfo(*p, &vkinfo))
{
//Sleep(200);
ExecuteKeyEvent(&vkinfo);
}
else
{
return FALSE;
}
}
return TRUE;
}