KEYBOARDDLL_API int SendKey(const HWND hwnd, const RECT rect, UINT vkey,
							const HWND restore_focus, const POINT restore_cursor)
{
	INPUT			input[4];

	POINT pt = RandomizeClickLocation(rect);
	double fScreenWidth = ::GetSystemMetrics( SM_CXSCREEN )-1;
	double fScreenHeight = ::GetSystemMetrics( SM_CYSCREEN )-1;

	// Translate click point to screen/mouse coords
	ClientToScreen(hwnd, &pt);
	double fx = pt.x*(65535.0f/fScreenWidth);
	double fy = pt.y*(65535.0f/fScreenHeight);

	// Set up the input structure
	int input_count=0;

	// First click in the rect to select it, if rect is not passed in as {-1, -1, -1, -1}
	if (rect.left!=-1 || rect.top!=-1 || rect.right!=-1 || rect.bottom!=-1)
	{
		ZeroMemory(&input[input_count],sizeof(INPUT));
		input[input_count].type = INPUT_MOUSE;
		input[input_count].mi.dx = (LONG) fx;
		input[input_count].mi.dy = (LONG) fy;
		input[input_count].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;
		input_count++;
		
		ZeroMemory(&input[input_count],sizeof(INPUT));
		input[input_count].type = INPUT_MOUSE;
		input[input_count].mi.dx = (LONG) fx;
		input[input_count].mi.dy = (LONG) fy;
		input[input_count].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTUP;
		input_count++;
	}

	// Add vkey to the input struct
	ZeroMemory(&input[input_count],sizeof(INPUT));
	input[input_count].type = INPUT_KEYBOARD;
	input[input_count].ki.wVk = vkey;
	input_count++;

	ZeroMemory(&input[input_count],sizeof(INPUT));
	input[input_count].type = INPUT_KEYBOARD;
	input[input_count].ki.wVk = vkey;
	input[input_count].ki.dwFlags = KEYEVENTF_KEYUP;
	input_count++;

	// Set focus to target window
	SetFocus(hwnd);
	SetForegroundWindow(hwnd);
	SetActiveWindow(hwnd);

	// Send input
	SendInput(input_count, input, sizeof(INPUT));

	// Restore previous window state and cursor position
	if (restore_focus!=NULL)
	{
		SetActiveWindow(restore_focus);
		SetForegroundWindow(restore_focus);
		SetFocus(restore_focus);
	}

	// Remove that code-block, if you don't want to restore the mouse-cursor!
	if (restore_cursor.x!=-1 && restore_cursor.y!=-1)
	{
		SetCursorPos(restore_cursor.x, restore_cursor.y);
	}

	return (int) true;
}
KEYBOARDDLL_API int SendString(const HWND hwnd, const RECT rect, const CString s, const bool use_comma,
							   const HWND restore_focus, const POINT restore_cursor)
{
	INPUT			input[102];

	POINT pt = RandomizeClickLocation(rect);
	double fScreenWidth = ::GetSystemMetrics( SM_CXSCREEN )-1;
	double fScreenHeight = ::GetSystemMetrics( SM_CYSCREEN )-1;

	// Translate click point to screen/mouse coords
	ClientToScreen(hwnd, &pt);
	double fx = pt.x*(65535.0f/fScreenWidth);
	double fy = pt.y*(65535.0f/fScreenHeight);

	// Set up the input structure

	int input_count=0;

	// First click in the rect to select it, if rect is not passed in as {-1, -1, -1, -1}
	if (rect.left!=-1 || rect.top!=-1 || rect.right!=-1 || rect.bottom!=-1)
	{
		ZeroMemory(&input[input_count],sizeof(INPUT));
		input[input_count].type = INPUT_MOUSE;
		input[input_count].mi.dx = (LONG) fx;
		input[input_count].mi.dy = (LONG) fy;
		input[input_count].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;
		input_count++;
		
		ZeroMemory(&input[input_count],sizeof(INPUT));
		input[input_count].type = INPUT_MOUSE;
		input[input_count].mi.dx = (LONG) fx;
		input[input_count].mi.dy = (LONG) fy;
		input[input_count].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTUP;
		input_count++;
	}

	// Set focus to target window
	SetFocus(hwnd);
	SetForegroundWindow(hwnd);
	SetActiveWindow(hwnd);

	// Send input
	SendInput(input_count, input, sizeof(INPUT));

	// Send each character of the string via PlayKeyboardEvent
	char ch_str[100];
	sprintf_s(ch_str, 100, "%s", s.GetString());

	int	vkey = 0;

	int i = 0, strlength = (int)strlen(ch_str);
	short KeyScan;
	for (int i=0; i<strlength; i++)
	{
		Sleep(20);
		if (use_comma && ch_str[i]=='.')
			ch_str[i] = ',';
		KeyScan = VkKeyScan(ch_str[i]);
		PlayKeyboardEvent(LOBYTE(KeyScan), HIBYTE(KeyScan));
	}
	Sleep(20);

	// Restore previous window state and cursor position
	if (restore_focus!=NULL)
	{
		SetActiveWindow(restore_focus);
		SetForegroundWindow(restore_focus);
		SetFocus(restore_focus);
	}

	// Remove that code-block, if you don't want to restore the mouse-cursor!
	if (restore_cursor.x!=-1 && restore_cursor.y!=-1)
	{
		SetCursorPos(restore_cursor.x, restore_cursor.y);
	}

	return (int) true;
}
Beispiel #3
0
MOUSEDLL_API int MouseClick(const HWND hwnd, const RECT rect, const MouseButton button, const int clicks,
							const HWND restore_focus, const POINT restore_cursor)
{
	INPUT			input[100] = {0};

	POINT pt = RandomizeClickLocation(rect);
	double fScreenWidth = ::GetSystemMetrics( SM_CXSCREEN )-1;
	double fScreenHeight = ::GetSystemMetrics( SM_CYSCREEN )-1;

	// Translate click point to screen/mouse coords
	ClientToScreen(hwnd, &pt);
	double fx = pt.x*(65535.0f/fScreenWidth);
	double fy = pt.y*(65535.0f/fScreenHeight);

	// Set up the input structure
	for (int i = 0; i<clicks*2; i+=2)
	{
		ZeroMemory(&input[i],sizeof(INPUT));
		input[i].type = INPUT_MOUSE;
		input[i].mi.dx = (LONG) fx;
		input[i].mi.dy = (LONG) fy;

		switch (button)
		{
		case MouseLeft:
			input[i].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;
			break;
		case MouseMiddle:
			input[i].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_MIDDLEDOWN;
			break;
		case MouseRight:
			input[i].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_RIGHTDOWN;
			break;
		}
		
		ZeroMemory(&input[i+1],sizeof(INPUT));
		input[i+1].type = INPUT_MOUSE;
		input[i+1].mi.dx = (LONG) fx;
		input[i+1].mi.dy = (LONG) fy;

		switch (button)
		{
		case MouseLeft:
			input[i+1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTUP;
			break;
		case MouseMiddle:
			input[i+1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_MIDDLEUP;
			break;
		case MouseRight:
			input[i+1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_RIGHTUP;
			break;
		}
	}

	// Set focus to target window
	SetFocus(hwnd);
	SetForegroundWindow(hwnd);
	SetActiveWindow(hwnd);

	// Send input
	Sleep(100);
	SendInput(clicks*2, input, sizeof(INPUT));
	Sleep(100);

	// Restore previous window state and cursor position
	if (restore_focus!=NULL)
	{
		SetActiveWindow(restore_focus);
		SetForegroundWindow(restore_focus);
		SetFocus(restore_focus);
	}

	// Remove that code-block, if you don't want to restore the mouse-cursor!
	if (restore_cursor.x!=-1 && restore_cursor.y!=-1)
	{
		SetCursorPos(restore_cursor.x, restore_cursor.y);
	}

	return (int) true;
}