C++ Send Keys Background

05/08/2014 16:09 reload!#1
Hello guys :) I've a problem, how i can send key to inactive window? For example how i can send "x" in background to inactive window? Thanks and sorry for my bad english xD
05/08/2014 18:05 _Entropia_#2
u can use SendMessage or PostMessage to send keyboard events.
05/10/2014 19:39 Accelerate.#3
As Entropia said, Use SendMessage. You can do it followed by WM_LBUTTON.
Code:
LRESULT WINAPI SendMessage(
  _In_  HWND hWnd,
  _In_  UINT Msg,
  _In_  WPARAM wParam,
  _In_  LPARAM lParam
);
You'll have something like this then:
Code:
#include <Windows.h>
HWND window = FindWindow(0, L"Your Window Title");
SendMessage(window, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
SendMessage(window, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x, y));
EDIT; I'm sorry I thought you wanted to send a mouseclick.
Well, here this is how you send the "x" key:
Code:
HWND window = FindWindow(0, L"Your Window Title");
SendMessage(window, WM_CHAR, 'x', 0);
Also, if you want to send a keystroke with SendMessage you can do something like this:
Code:
void sendText(LPCWSTR windowName, char* szText)
{
	if (strlen(szText) < 1) return;

	for (int i = 0; i < strlen(szText); ++i)
	{
		HWND hwnd = FindWindow(0, windowName);
		SendMessage(hwnd, WM_CHAR, szText[i], 0);
	}
}
Code:
sendText(L"Your Window Name", "some text here");
05/11/2014 20:57 reload!#4
Ok Thanks guys :)