C语言实现模拟键盘按键事件:
#include <stdio.h>
#include <windows.h>
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
int main(void)
{
POINT pt;
int i;
int delaytime;
int keynum;
printf("input the delay time(ms):");
scanf("%d",&delaytime);
while(1)
{
switch (KEYDOWN(VK_ESCAPE))
{
case 1:
{
while (!KEYDOWN(VK_RETURN ))
{
Sleep(10);
}
break;
}
case 0:
{
//VK_NUMLOCK
//VK_SPACE
keybd_event(VK_NUMLOCK , 0, 0, 0);
Sleep(delaytime/2);
keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0);
Sleep(delaytime/2);
break;
}
}
}
return 0;
}
主要是用到了keybd_event(VK_NUMLOCK , 0, 0, 0);这个函数。关于函数的解释请到百度了自己搜。
利用switch和while巧妙实现了按ESC键暂停,按Enter键继续。