C語言實現模擬鍵盤按鍵事件
阿新 • • 發佈:2019-01-28
#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鍵繼續。
剛開啟軟體時需要輸入按鍵事件之間的時間延遲,單位是ms。