1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include "stdafx.h"
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
void PressKey(int vk) {
printf("sending input\n");
KEYBDINPUT kb = {0};
INPUT Input = {0};
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
printf("ERRORS: %d\n",GetLastError());
}
void MessageLoop()
{
BOOL bRet;
MSG msg = {0};
while ( (bRet = GetMessage(&msg, NULL,WM_HOTKEY, WM_HOTKEY)) != 0)
{
if(bRet== -1){
printf("ERROR %d\n",GetLastError());
}
else{
if (msg.message == WM_HOTKEY)
{
printf("msg.message %d, msg.lParam %d, msg.wParam %d \n",msg.message,msg.lParam,msg.wParam);
PressKey(98);
}
}
}
printf("message loop ended\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
Sleep(2000);
//this works
PressKey(97);
RegisterHotKey(NULL,1,MOD_CONTROL | MOD_ALT,0x41); //0x41 is 'a'
RegisterHotKey(NULL,2,MOD_CONTROL | MOD_ALT,0x5a); //0x5a is 'z'
MessageLoop();
//this does not work
PressKey(99);
UnregisterHotKey(NULL,1);
UnregisterHotKey(NULL,2);
return 0;
}
|