SendInput模拟键盘操作

#include <windows.h>

int main()
{
	HWND parentHwnd, childHwnd;
	INPUT input[4];
	parentHwnd = FindWindow(TEXT("Notepad"), NULL);
	if (parentHwnd)
	{
		childHwnd = FindWindowEx(parentHwnd, NULL, TEXT("Edit"), NULL);
		if (childHwnd)
		{
			for (int i = 0; i < 10; i++)
			{
				SendMessage(childHwnd, WM_CHAR, 'A', 0);
			}
			
			SetForegroundWindow(parentHwnd);

			//CTRL+S
			memset(input, 0, sizeof(input));
			input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
			input[0].ki.wVk = input[2].ki.wVk = VK_CONTROL;
			input[1].ki.wVk = input[3].ki.wVk = 0x53;
			input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
			SendInput(4, input, sizeof(INPUT));

			//ALT+F4
			memset(input, 0, sizeof(input));
			input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
			input[0].ki.wVk = input[2].ki.wVk = VK_MENU;
			input[1].ki.wVk = input[3].ki.wVk = VK_F4;
			input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
			SendInput(4, input, sizeof(INPUT));

		}
	}
	return 0;
}

/*
发送字符串
TCHAR *str = TEXT("Hello World");
SendMessage(childHwnd, WM_SETTEXT, 0, (LPARAM)str);

腾讯QQ
FindWindow(TEXT("TXGuiFoundation"), TEXT("N3verL4nd"));

*/


/*
POINT pt;
char *str = "Hello World";
while (1)
{
Sleep(1000);
GetCursorPos(&pt);
//hwnd = WindowFromPoint(pt);
hwnd = FindWindow(TEXT("Notepad"), NULL);
SendMessage(hwnd, WM_CHAR, (WPARAM)'G', NULL);
}
*/
实现操作:向已经打开的记事本写入数据,保存(CTRL+S),关闭(ALT+F4)。

尝试用SendMessage发送组合键,没有得到解决办法。


如果我们获取QQ窗口的HWND,那么我们就可以自动发消息了。


int main()
{
	HWND hwndTX, hwndConsole;
	INPUT input[4];
	hwndConsole = GetConsoleWindow();
	hwndTX = FindWindow(TEXT("TXGuiFoundation"), TEXT("N3verL4nd"));
	if (hwndTX != NULL)
	{
		for (int i = 0; i < 10; i++)
		{
			SendMessage(hwndTX, WM_CHAR, 'A', 0);
		}

		SetForegroundWindow(hwndTX);
		memset(input, 0, sizeof(input));
		input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
		input[0].ki.wVk = input[2].ki.wVk = VK_MENU;
		input[1].ki.wVk = input[3].ki.wVk = 0x53;
		input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
		SendInput(4, input, sizeof(INPUT));
		SetForegroundWindow(hwndConsole);
	}
	else
	{
		puts("not found");
	}
	return 0;
}




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值