SendInput模拟键盘操作
2016-06-08 20:52
375 查看
#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; }
相关文章推荐
- SendInput模拟键盘操作
- JavaScript---网络编程(5)-自定义对象Json、Dom模型概念讲解
- JavaScript---网络编程(5)-自定义对象Json、Dom模型概念讲解
- 【TRIO-Basic从入门到精通教程二】MPV4软件的使用
- 安恒堡垒机让您过一个轻松愉快的端午节
- Code Craft 2016初赛的一点体会
- 更新组合值
- 分治法
- 典型用户与场景
- Session解析
- UILabel 高度的自适应与获取
- C++实现多线程全局内存池(性能优化)
- java—异常处理机制
- 进程、线程(面试一)
- 第15.16周学习进度表
- poj 1064 Cable master
- 常规功能和模块自定义系统 (cfcmms)—049模块的条件限定
- 6.5 THUSC 考试题解
- 264. Ugly Number II
- Java 中的线程管理概念梳理