首先最简单的但可以后台处理的有SendMessage()PostMessage() 发送鼠标, 键盘消息, 这个百度一搜n多了 然后中等级的模拟就是keyboard_eventmouse_event了这个比较用的比较多, 给个例子你 模拟键盘点击void Press(UINT key)
{
keybd_event(key,MapVirtualKey(key, 0),0,0);
keybd_event(key,MapVirtualKey(key, 0),KEYEVENTF_KEYUP,0);
} 模拟 鼠标左键单击mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); 这里注意调用 keyboard_event 的时候一定要用MapVirtualKey 网上大部分的扰睁代码都是帆贺没用的, 这个函数时获取 硬件扫描码 的先说下keybd_event();函数的参数keybd_event(要模拟按下的虚拟按键码, 虚拟按键码对应的硬件扫描码,0,0);网上的代码大部分都有 虚拟按键码, 当然你运行大态李派部分都是没问题的, 因为对于一般的程序而言是没问题的但有的程序为了防止 外挂 或者 防止其他程序恶意修改什么的,会对 鼠标键盘 消息进行检测, 如果检测到没有硬件扫描码会拒绝执行的 然后还有比较高级的就是SendInput模拟了void OnSendCharCode(unsigned short unicode = 0,unsigned short vcode = 0,bool bDown = false,bool bUnicode = true);
void OnSendCharCode(unsigned short unicode,unsigned short vcode,bool bDown,bool bUnicode)
{
unsigned short uNum = 0;
// v-code Clicked INPUT 事件设定
KEYBDINPUT kDown;
KEYBDINPUT kUp;
if(!bUnicode)
{
// 'A'; (Virtual Code 只有大写), 要输入小写
// Enter: 13, Backspace: 8, Up:38
kDown.wVk = kUp.wVk = vcode;
kDown.wScan = kUp.wScan = ::MapVirtualKey(kDown.wVk,0);
//kDown.dwFlags = KEYEVENTF_EXTENDEDKEY;
kUp.dwFlags = KEYEVENTF_KEYUP;
}else{
kDown.wVk = kUp.wVk = 0;
kDown.wScan = kUp.wScan = unicode;
kDown.dwFlags = KEYEVENTF_UNICODE;
kUp.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
}
// 建立 KeyDown 事件 建立 KeyUP 事件
INPUT inputKeyDown, inputKeyUP;
inputKeyDown.type = inputKeyUP.type = INPUT_KEYBOARD; // 指定 input 为 keyboard
kDown.time = kUp.time = 0; // the system will provide its own time stamp.
kDown.dwExtraInfo = kUp.dwExtraInfo = (WORD)::GetMessageExtraInfo();
inputKeyDown.ki = kDown;// 指定 键盘 属性结构
inputKeyUP.ki = kUp; // 指定 键盘 属性结构
// v-code Clicked INPUT 事件设定完成
// 将 INPUT 事件送到 Root 视窗, 系统会根据视窗位置,送给目标视窗
if(bDown)
{
INPUT event[1] = {inputKeyDown};
uNum = ::SendInput(1,event, sizeof(INPUT));
}else{
INPUT event[2] = {inputKeyDown,inputKeyUP};
uNum = ::SendInput(2,event, sizeof(INPUT));
}
}
//例子:
OnSendCharCode(0,VK_SHIFT,true,false);
OnSendCharCode(0,'A',false,false);
OnSendCharCode(0,VK_SHIFT,false,false);
OnSendCharCode(0,'A',false,false);
//KEYEVENTF_UNICODE 能区分大小写
OnSendCharCode('A');
OnSendCharCode('a'); 不过还是有很多游戏,它是用 DirectX 技术去做的,这些大部分对他们没用所以还有最最最高级的模拟, 就是 对硬件驱动程序的模拟这个有个外国人写的 winio.h 的头文件, 有兴趣可以去学, 一般用不到的,
通过调敏罩念闷基用API mouse_event,SetCursorPos实现鼠标的模拟 给你点简单的代码自己研究下 想桥困自己动手是好事Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Command1_Click()
SetCursorPos Val(400), Val(0)
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End SubPrivate Sub Command2_Click()
mouse_event MOUSEEVENTF_MOVE, 210, 200, 0, 0
SetCursorPos Val(300), Val(300)
mouse_event MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub