Posting Messages

Hi,
I'm trying to make a program that can change user input.
When user hits 'L' key my program posts a message that 'K' key was pressed.
Something like this:
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
#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	if(message==WM_KEYDOWN)
		if(wParam=='L') PostMessage(hWnd, WM_KEYDOWN, 'K', 0);
	return DefWindowProc (hWnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
	HWND hWnd;
	WNDCLASSEX wc;

	memset(&wc, 0, sizeof(wc));
	wc.cbSize = sizeof(wc);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = "WindowClass";
	RegisterClassEx(&wc);

	hWnd = CreateWindowEx(NULL,"WindowClass","Window",
		WS_OVERLAPPEDWINDOW,300,300,500,400,0,0,hInstance,0);
	ShowWindow(hWnd, nCmdShow);

	MSG msg;
	while(!GetAsyncKeyState(VK_ESCAPE)){
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}

Basically this program works, but I now it only effects itself.
Could anyone tell me how to make it work with notepad and other applications?
Google around SetWindowHookEx(). You'll need to create a system-wide (or 'global') hook -- meaning it will have to exist in a DLL that your application will load.

You can also tell windows to change your keyboard layout directly. See
http://www.google.com/search?btnI=1&q=msdn+Windows+Keyboard+Layouts
(The "Related Links" on the right side are of interest.)

Good luck!
Last edited on
> meaning it will have to exist in a DLL that your application will load.

No, useless with LL hooks...
Topic archived. No new replies allowed.