zoukankan      html  css  js  c++  java
  • 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;
    }




    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    [BZOJ]1018 堵塞的交通(SHOI2008)
    [BZOJ]1069 最大土地面积(SCOI2007)
    HDU5739:Fantasia——题解
    洛谷6186:[NOI Online 提高组]冒泡排序——题解
    洛谷4631 & UOJ415 & LOJ2586:[APIO2018] Circle selection 选圆圈——题解
    洛谷2014:[CTSC1997]选课——题解
    洛谷2758:编辑距离——题解
    洛谷4148 & BZOJ4066:简单题——题解
    洛谷4357 & BZOJ4520:[CQOI2016]K远点对——题解
    洛谷4320:道路相遇——题解
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834698.html
Copyright © 2011-2022 走看看