1,keybd_event的使用
1.1 模拟输入字母
例子:模拟输入字母a(小写)
keybd_event('A',0,0,0);
keybd_event('A',0,KEYEVENTF_KEYUP,0);
模拟输入字母A
keybd_event(VK_SHIFT,0,0,0);
keybd_event('A',0,0,0);
keybd_event('A',0,KEYEVENTF_KEYUP,0);
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);
(其它字母的大小写依此类推)
模拟输入数字
模拟输入数字‘0’
keybd_event(‘0’,0,0,0);
keybd_event(‘0’,0,KEYEVENTF_KEYUP,0);
模拟输入数字‘1’
keybd_event('1',0,0,0);
keybd_event('1',0,KEYEVENTF_KEYUP,0);
1.2下面是一个自动打开一个记事本并在里面写字母数字的程序
HWND hprocess = ::FindWindow("Notepad","新建 文本文档.txt - 记事本");
if(!hprocess)
{
MessageBox("没有找到记事本");
return;
}
if(::IsIconic(hprocess))
{
::ShowWindow(hprocess,SW_SHOWNORMAL);
}
::BringWindowToTop(hprocess);
::SetFocus(hprocess);
CRect rect;
::GetWindowRect(hprocess,&rect);
mouse_event(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE,(rect.TopLeft().x+100)*65535/1280,(rect.TopLeft().y+100)*65535/800,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(1000);
for(int i=0;i<=10;i++)
{
keybd_event(VK_SHIFT,0,0,0);
keybd_event('A',0,0,0);
keybd_event('A',0,KEYEVENTF_KEYUP,0);
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);
keybd_event('1',0,0,0);
keybd_event('1',0,KEYEVENTF_KEYUP,0);
Sleep(500);
}