Simulate Input with windows low level keyboard hook

Astoundingly MSDN says Windows hooks can be used to simulate input, but I've yet to figure out how. A google search doesn't turn up anything promising, either. (This is a low level keyboard hook)

So, here's what I have:
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
//make sure system is windows.
#include <windows.h>

LRESULT CALLBACK newKeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
        //these values came from
        //a different hook which saved values
        //of WM_ down & up to a file.
	KBDLLHOOKSTRUCT kbd;
	ZeroMemory(&kbd,sizeof(KBDLLHOOKSTRUCT));
	kbd.dwExtraInfo = 0;
	kbd.flags = 0;
	kbd.scanCode = 30;
	kbd.time = GetMessageTime();
	kbd.vkCode = 65;
        // Below is my futile attempt to have the normal hook
        //process my fake message.
	CallNextHookEx(NULL,0, WM_KEYDOWN, reinterpret_cast<LPARAM>(&kbd));
	kbd.flags = 128;
	kbd.time = GetMessageTime();
	CallNextHookEx(NULL,0, WM_KEYUP, reinterpret_cast<LPARAM>(&kbd));

	return CallNextHookEx(NULL,nCode,wParam,lParam);
}


int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
	HHOOK keyboardHook = 
		SetWindowsHookEx(WH_KEYBOARD_LL,
		newKeyboardHookProc,
		hInstance,
		NULL);
   MessageBox(NULL, L"Press OK to close.", L"", MB_OK);
   
   UnhookWindowsHookEx(keyboardHook);
   return 0;
}


Fair word of warning, if this actually works it will spam the 'a' key until you click OK.

Basically it doesn't work at all, and I'm wondering why. This MSDN page leads me to believe it's possible to simulate input with this hook, (maybe I'm using the wrong type of hook?) : http://msdn.microsoft.com/en-us/library/ms644959%28v=VS.85%29.aspx

Topic archived. No new replies allowed.