第1个回答 2011-09-14
函数原型:
VOID keybd_event( BYTE bVk, BYTE bScan, DWORD dwFlags, DWORD dwExtraInfo );
4个参数都是输入参数。
bVk:键名,例如: VK_NUMPAD7,VK_BACK, VK_TAB,VK_SHIFT,VK_F1,。。。
keybd_event(VK_MENU,0xb8,0 , 0); -- 按下Alt 键
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); -- 放开 Alt 键 (复位,弹上来)
===
下面程序模拟按 notepad 编辑器。等于C语言 system("notepad"); 弹出 记事本。
void simu_notepad()
{
keybd_event(VkKeyScan('N'),1,0,0);
keybd_event(VkKeyScan('N'),1,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('O'),0x98,0,0); keybd_event(VkKeyScan('O'),0x98,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('T'),0x94,0,0); keybd_event(VkKeyScan('T'),0x94,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('E'),0x92,0,0); keybd_event(VkKeyScan('E'),0x92,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('P'),0x99,0,0); keybd_event(VkKeyScan('P'),0x99,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('A'),0x9E,0,0); keybd_event(VkKeyScan('A'),0x9E,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('D'),0xA0,0,0); keybd_event(VkKeyScan('D'),0xA0,KEYEVENTF_KEYUP,0);
keybd_event(VK_RETURN,1,0,0);
keybd_event(VK_RETURN,1,KEYEVENTF_KEYUP,0);
}本回答被提问者和网友采纳
第2个回答 2015-10-03
模拟鼠标如下:
1、这里是鼠标左键按下和松开两个事件的组合即一次单击:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
2、模拟鼠标右键单击事件:
mouse_event (MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 )
3、两次连续的鼠标左键单击事件 构成一次鼠标双击事件:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
4、使用绝对坐标
MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0
模拟键盘输入如下:
void EnterString(const char* text)
{
bool bShift = false;
size_t length = strlen(text);
for(size_t index = 0;index < length;index++)
{
int val = text[index];
if(val >= 97 && val <= 122) //小写a-z
{
val -= 32;
bShift = false;
}
else if(val >= 65 && val <= 90) //大写A-Z
{
bShift = true;
}
else if(val >= 48 && val <= 57) //数字0-9
{
bShift = false;
}
else //特殊符号
{
switch(val)
{
case '`':
val = 192;
bShift = false;
break;
case '~':
val = 192;
bShift = true;
break;
case '!':
val = '1'; //!和1在同一个键盘,使用同一个键盘码,以下相同
bShift = true;
break;
case '@':
val = '2'; //@和2在同一个键盘,使用同一个键盘码,以下相同
bShift = true;
break;
case '#':
val = '3';
bShift = true;
break;
case '$':
val = '4';
bShift = true;
break;
case '%':
val = '5';
bShift = true;
break;
case '^':
val = '6';
bShift = true;
break;
case '&':
val = '7';
bShift = true;
break;
case '*':
val = '8';
bShift = true;
break;
case '(':
val = '9';
bShift = true;
break;
case ')':
val = '0';
bShift = true;
break;
case '-':
val = 189;
bShift = false;
break;
case '=':
val = 187;
bShift = false;
break;
case '_':
val = 189;
bShift = true;
break;
case '+':
val = 187;
bShift = true;
break;
case '[':
val = 219;
bShift = false;
break;
case '{':
val = 219;
bShift = true;
break;
case ']':
val = 221;
bShift = false;
break;
case '}':
val = 221;
bShift = true;
break;
case '\\':
val = 220;
bShift = false;
break;
case '|':
val = 220;
bShift = true;
break;
case ';':
val = 186;
bShift = false;
break;
case ':':
val = 186;
bShift = true;
case '\'':
val = 222;
bShift = false;
break;
case '\"':
val = 222;
bShift = true;
break;
case ',':
val = 188;
bShift = false;
break;
case '<':
val = 188;
bShift = true;
break;
case '.':
val = 190;
bShift = false;
break;
case '>':
val = 190;
bShift = true;
break;
case '/':
val = 191;
bShift = false;
break;
case '?':
val = 191;
bShift = true;
break;
default:
break;
}
}
if(bShift)
keybd_event(VK_SHIFT,0,0,0);
keybd_event(val,0,0,0);
keybd_event(val,0,KEYEVENTF_KEYUP,0);
if(bShift)
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);
}
}