这里是模拟键盘的主要代码:
void NotifyKBEvent(wchar_t ch)
{
SHORT vks = VkKeyScanW(ch);
BYTE vk = LOBYTE(vks);
BYTE Shift = HIBYTE(vks);
if (vk == (BYTE)-1/* || Shift == (BYTE)-1*/)
{//UNICODE 字符
INPUT input[2];
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0;
input[0].ki.wScan = ch;
input[0].ki.dwFlags = 0x4;//KEYEVENTF_UNICODE;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = 0;
input[1].ki.wScan = ch;
input[1].ki.dwFlags = KEYEVENTF_KEYUP | 0x4;//KEYEVENTF_UNICODE;
SendInput(2, input, sizeof(INPUT));
}
else
{//
if (Shift)
{
INPUT input[4] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = Shift;//VK_SHIFT;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = ch;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = ch;
input[2].ki.dwFlags = KEYEVENTF_KEYUP;
input[3].type = INPUT_KEYBOARD;
input[3].ki.wVk = Shift;//VK_SHIFT;
input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, input, sizeof(INPUT));
}
else
{
INPUT input[2] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = vks;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = vks;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
}
}
}
void NotifyKBEvent(wchar_t* chs)
{
if (chs == NULL)
return ;
while(*chs) NotifyKBEvent(*chs++);
}
void SendKBEvent(WORD wVk, DWORD dwFlags = 0, DWORD dwExtraInfo = 0)
{
INPUT input[1] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = wVk;
input[0].ki.wScan = MapVirtualKey(wVk, 0);
input[0].ki.dwFlags = dwFlags;
input[0].ki.dwExtraInfo = dwExtraInfo;
input[0].ki.time = GetTickCount();
SendInput(1, input, sizeof(INPUT));
}
//这里是处理标题栏和不活动代码:
//去掉任务栏图标 和 始终不处于活动状态
ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW | 0x08000000);
//初始不活动
SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
源码:点击打开链接