I really need help. I'm pretty new to c++. I want to send a simple keystroke to notepad (not really notepad, but I'm using it while I learn).

I might have forgotten something, I don't know. I have a feeling it's some retarded mistake...

#include <windows.h>
#define VK_A 0x9E

void SendKeys(HWND notepad);

void SendKeys(HWND notepad) {
	PostMessage(notepad, WM_KEYDOWN, VK_A, 1);
}

int main() {
	HWND notepad = FindWindow(NULL, L"Bajs.txt - Anteckningar");
	SendKeys (notepad);
	return 0;
}

I think I know what the problem is really, I need to get a keyboard handle on the window. But what do I use for that? I have tryed:

SetFocus(notepad);

From what I can understand SetFocus only works if notepad is in the same processthread as my program. Is there a way to get keyboard handle on a program that is not in the same thread?

Thats not the mistake, the problem is that your trying to send keypresses to the window itself, not the text box inside it, you will have to get the handle to the text box in order for that to work.

Once you have the handle to the text box (should be defined like this)

HWND hEdit = ...;

you can use SetWindowText to edit the text in the text box.

Once you have the handle to the text box (should be defined like this)

HWND hEdit = ...;

you can use SetWindowText to edit the text in the text box.

I see, now I understand, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.