[方法记录]向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,窗口名为网易有道词典。

相关推荐
PieroPc6 小时前
Windows 驱动备份与恢复工具 CMD .bat
windows
波罗丁牌6 小时前
逆变与协变详解
windows·microsoft
映翰通朱工7 小时前
Windows 上位机 + EC3320:USB 转 CAN 通信实测(从接线到 SSH 收包)
windows·stm32·ssh
我是Superman丶7 小时前
【2026亲测】Wireshark安装教程 Windows系统|下载安装、Npcap配置及首次抓包
windows·测试工具·wireshark
sg_knight9 小时前
Codex CLI 安装全攻略:macOS / Linux / Windows(WSL2)三端实战
linux·windows·macos·openai·ai编程·coding·codex
清水迎朝阳1 天前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
趣味科技1 天前
记录修复EAGET忆捷移动硬盘损坏
windows·数据分析·硬件工程
我命由我123451 天前
Java 开发 - List subList 方法
java·windows·操作系统·list·intellij-idea·idea·intellij idea
吃辣我第一1 天前
监听SHP目录自动注册SuperMap iServer地图服务(Windows版)
windows·iserver·shapefile
程序员老陆1 天前
FFmpeg6 在 Windows 打开麦克风并录成 PCM:不走 Qt Multimedia,也不碰 WASAPI
windows·ffmpeg·音视频·pcm