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
    知识共享,欢迎转载。
  • 相关阅读:
    杜教筛
    GCD Counting Codeforces
    洛谷 P4317 花神的数论题 || bzoj3209
    About set HDU
    Queue Sequence HDU
    bzoj2154||洛谷P1829 Crash的数字表格&&JZPTAB && bzoj3309 DZY Loves Math
    洛谷 P1445 [Violet]樱花
    洛谷 P2158 [SDOI2008]仪仗队 && 洛谷 P1447 [NOI2010]能量采集
    nginx中使用waf防火墙
    wordpress安装
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834698.html
Copyright © 2011-2022 走看看