[方法记录]向Windows的特定窗口注入键盘事件

前言

在尝试开发一款可以远程控制电脑进行文字输入软件时候,需要找到合适方法在Windows端注入键盘事件。查询微软的相关文档之后,发现通过两个简单的函数就能实现基本的键盘事件注入功能。

事件注入

使用SendInput方法即可。参照官方给出的实例:

cpp 复制代码
//**********************************************************************
//
// Sends Win + D to toggle to the desktop
//
//**********************************************************************
void ShowDesktop()
{
    OutputString(L"Sending 'Win-D'\r\n");
    INPUT inputs[4] = {};
    ZeroMemory(inputs, sizeof(inputs));

    inputs[0].type = INPUT_KEYBOARD;
    inputs[0].ki.wVk = VK_LWIN;
   
    inputs[1].type = INPUT_KEYBOARD;
    inputs[1].ki.wVk = 'D';

    inputs[2].type = INPUT_KEYBOARD;
    inputs[2].ki.wVk = 'D';
    inputs[2].ki.dwFlags = KEYEVENTF_KEYUP;

    inputs[3].type = INPUT_KEYBOARD;
    inputs[3].ki.wVk = VK_LWIN;
    inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;

    UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
    if (uSent != ARRAYSIZE(inputs))
    {
        OutputString(L"SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
    } 
}

写成如下函数进行测试:

cpp 复制代码
UINT sendInput(WORD virtualKeyCode) {
    cout<<"sendInput"<<virtualKeyCode<<endl;
    INPUT inputs[2] = {};
    ZeroMemory(inputs, sizeof(inputs));

    inputs[0].type = INPUT_KEYBOARD;
    inputs[0].ki.wVk = virtualKeyCode;

    inputs[1].type = INPUT_KEYBOARD;
    inputs[1].ki.wVk = virtualKeyCode;
    inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;

    UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
    if (uSent != ARRAYSIZE(inputs))
    {
        printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
    }

    return uSent;
}

int main()
{
    sendInput('W');
    sendInput('E');
    sendInput('B');
}

根据函数返回值,事件在这个时候已经注入成功了。

使用SetForegroundWindow 聚焦窗口

然而,事件被成功注入不代表被注入到我们想要的地方。sendInput方法只会将事件注入到已经聚焦的窗口,如果窗口没有聚焦的话是收不到注入的消息的。通过SetForegroundWindow方法,可以将特定的窗口聚焦。为了找到我们想聚焦的窗口,可以使用FindWindow方法。FindWindow需要两个参数,依次是类名、窗口名。 用法如下:

cpp 复制代码
HWND targetWindow = FindWindow("YodaoMainWndClass",NULL);
if (targetWindow != NULL)
{
    std::cout << "Window found!" << std::endl;
    SetForegroundWindow(targetWindow);
    
    
}

如何获取窗口的类名与方法名?

一般的窗口使用Visual Studio的工具Spy++即可。以网易有道词典为例:

如图可以看到,类名为YodaoMainWindClass,窗口名为网易有道词典。

相关推荐
海天胜景12 小时前
编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET... 拒绝访问
c语言·windows
搏博21 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
有梦想的攻城狮1 天前
Java 11中的Collections类详解
java·windows·python·java11·collections
忒可君1 天前
C# winform FTP功能
开发语言·windows·c#
十五年专注C++开发1 天前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建
degree5201 天前
全平台轻量浏览器推荐|支持Win/macOS/Linux,极速加载+隐私保护+扩展插件,告别广告与数据追踪!
windows·macos·电脑
许泽宇的技术分享2 天前
Windows桌面自动化的革命性突破:深度解析Windows-MCP.Net Desktop模块的技术奥秘
windows·自动化·.net
七仔的博客3 天前
【摸鱼办公神器】七仔的桌面工具超进化 -> 灵卡面板 v1.1.9
windows·神器·摸鱼
码农阿豪3 天前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
CC__xy3 天前
demo 通讯录 + 城市选择器 (字母索引左右联动 ListItemGroup+AlphabetIndexer)笔记
windows