char WM_KEYDOWN在硬编码时显示为数字而没有大写字母

char WM_KEYDOWN showing up as number and no capitol letters when hard coded

本文关键字:数字 大写字母 显示 WM KEYDOWN 编码 char      更新时间:2023-10-16

我正在写一个可以发送按键到另一个应用程序的应用程序。当我硬编码我想要发送的文本时,我有一些它在工作。然而,当我使用argv[]从参数列表中获取文本时,它不再发送文本,而是数字或什么都不显示。我认为问题在于从char*中获取文本,并在解析之前将其放入std::string中,并将每个字符发送到将击键发送到等待应用程序的函数。

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <cstdlib>
#include <string>
using namespace std;
HWND SendIt (DWORD dwProcessID, const char key)
{
    HWND hwnd = NULL;
    do {
        hwnd = FindWindowEx(NULL, hwnd, NULL, NULL);
        DWORD dwPID = 0;
        GetWindowThreadProcessId(hwnd, &dwPID);
        if (dwPID == dwProcessID) {
            PostMessage(hwnd, WM_KEYDOWN,key, 1);
        }
    } while (hwnd != 0);
    return hwnd;
}
int main(int argc, char *argv[]) {
    if (argc != 3) {
        cout << "Please provide a PID# and a String of Charaters to pass to the PID#.n";
        cout << "Examples:n";
        cout << "RegTSM.exe 16855 WP3C94n";
        cout << "RegTSM.exe 16855 "type this into the PID Handle" n";
        exit(1);
    }
    else {
        DWORD PID;
        std::string sPID;
        std::string text;
        sPID = argv[1];
        PID = atof(sPID.c_str());
        text = argv[2];
        text += "r";
        //std::string text = "WN403Ar";
        for (char & key : text) {
            SendIt(PID, key);
        }
    }
    return 0;
}

如果我运行上面的代码并提供一个cmd.exe进程的PID和一些像字母'a'这样的文本,它将在我所针对的cmd.exe进程中打印1。

如果我使用注释部分std::string text = "WN403Ar";作为for (char & key : text)的输入,那么它将正常工作(除了不产生我需要的大写字母)并键入wn403a

  1. 命令行参数不产生提供的实际字符。
  2. 即使字符串是硬编码,输出到另一个cmd.exe窗口也不是大写字母。

提前感谢!!

正如Raymond Chen博客所说:

你不能用PostMessage模拟键盘输入。

即使你可以可靠地做到这一点,你的代码也不是:

  1. 考虑目标进程有多个窗口的可能性。甚至当你正在发送击键时,它可能会在窗口之间切换。你正在发送你的关键消息到进程中的随机窗口,甚至只发送到顶级窗口,而不是发送时具有键盘输入的任何特定子控制窗口。

  2. 发送任何WM_KEYUP消息,只能发送WM_KEYDOWN消息

  3. 发送虚拟密钥码。WM_KEYDOWN/UP不操作字符,而是操作键码和扫描码。另一方面,WM_CHAR对字符进行操作。

  4. 考虑小写和大写,这需要模拟在发送大写字符时按住Shift键。

你真的应该重新考虑你的方法。完全不要使用PostMessage(),您应该使用SendInput()。然而,它不允许你瞄准一个特定的窗口,所以你必须使用WM_SETTEXT或甚至UI自动化将你的文本直接发送到目标窗口。

话虽这么说,如果你坚持使用PostMessage(),那么试试这样做:

#include "stdafx.h"
#include <windows.h>
#include <psapi.h>
#include <iostream>
#include <string>
#include <cstdio>
#include <cctype>
struct MyEnumInfo
{
    DWORD dwProcessID;
    HWND hwndFocus;
};
BOOL CALLBACK FindFocusedWindow(HWND hwnd, LPARAM lParam)
{
    MyEnumInfo *mei = (MyEnumInfo*)lParam;
    DWORD dwPID = 0;
    DWORD dwThreadID = GetWindowThreadProcessId(hwnd, &dwPID);
    if (dwPID == mei->dwProcessID)
    {
        GUITHREADINFO gti = {0};
        gti.cbSize = sizeof(gti);
        if (GetGUIThreadInfo(dwThreadID, &gti))
        {
            mei->hwndFocus = gti.hwndFocus;
            if (mei->hwndFocus)
                return FALSE;
        }
    }
    return TRUE;
}
HWND SendIt(DWORD dwProcessID, const std::string &text)
{
    if (text.empty())
        return NULL;
    MyEnumInfo mei;
    mei.dwProcessID = dwProcessID;
    mei.hwndFocus = NULL;
    EnumWindows(&FindFocusedWindow, (LPARAM)&mei);
    if (!mei.hwndFocus)
        return NULL;
    LPARAM lParamShift = MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC) << 16;
    LPARAM lParamCtrl = MapVirtualKey(VK_CONTROL, MAPVK_VK_TO_VSC) << 16;
    LPARAM lParamAlt = MapVirtualKey(VK_MENU, MAPVK_VK_TO_VSC) << 16;
    for (const char& key : text)
    {
        SHORT code = VkKeyScanA(key);
        BYTE vk = LOBYTE(code);
        BYTE shift = HIBYTE(code);
        if ((vk != -1) && (shift != -1))
        {
            if (shift & 1) PostMessage(mei.hwndFocus, WM_KEYDOWN, VK_SHIFT, lParamShift);
            if (shift & 2) PostMessage(mei.hwndFocus, WM_KEYDOWN, VK_CONTROL, lParamCtrl);
            if (shift & 4) PostMessage(mei.hwndFocus, WM_KEYDOWN, VK_MENU, lParamAlt);
            LPARAM lParamKey = MapVirtualKey(vk, MAPVK_VK_TO_VSC) << 16;
            PostMessage(mei.hwndFocus, WM_KEYDOWN, vk, lParamKey);
            PostMessage(mei.hwndFocus, WM_KEYUP, vk, lParamKey | 0xC0000001);
            if (shift & 1) PostMessage(mei.hwndFocus, WM_KEYUP, VK_SHIFT, lParamShift | 0xC0000001);
            if (shift & 2) PostMessage(mei.hwndFocus, WM_KEYUP, VK_CONTROL, lParamCtrl | 0xC0000001);
            if (shift & 4) PostMessage(mei.hwndFocus, WM_KEYUP, VK_MENU, lParamAlt | 0xC0000001);
        }
    }
    return mei.hwndFocus;
}
void usage()
{
    std::cout << "Please provide a PID# and a String of Characters to send to the PID#.n";
    std::cout << "Examples:n";
    std::cout << "RegTSM.exe 16855 WP3C94n";
    std::cout << "RegTSM.exe 16855 "type this into the PID Handle"n";
    exit(1);
}
int main(int argc, char *argv[]) {
    if (argc != 3) {
        usage();
        return 1;
    }
    DWORD PID;
    if (std::sscanf(argv[1], "%u", &PID) != 0) {
        usage();
        return 1;
    }
    std::string text = std::string(argv[2]) + "r";
    //std::string text = "WN403Ar";
    SendIt(PID, text);
    return 0;
}

自从我访问这个项目以来已经有一段时间了,但是我想发布最终工作的代码。感谢所有的评论和教育!!

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <cstdlib>
#include <string>
using namespace std;
HWND SendIt (DWORD dwProcessID, const char key)
{
    HWND hwnd = NULL;
    do {
        hwnd = FindWindowEx(NULL, hwnd, NULL, NULL);
        DWORD dwPID = 0;
        DWORD threadId = GetWindowThreadProcessId(hwnd, &dwPID);
        if (dwPID == dwProcessID) {
            PostMessage(hwnd, WM_CHAR, (WPARAM)key, 0);
        }
    } while (hwnd != 0);
    return hwnd;
}
int main(int argc, char *argv[]) {
    if (argc != 3) {
        cout << "Please provide a PID# and a String of Charaters to pass to the PID#.n";
        cout << "Examples:n";
        cout << "RegTSM.exe 16855 WP3C94n";
        cout << "RegTSM.exe 16855 "type this into the PID Handle" n";
        exit(1);
    }
    else {
        DWORD PID;
        std::string sPID;
        std::string text;
        sPID = argv[1];
        PID = atof(sPID.c_str());
        text = argv[2];
        text += "r";
        for (char & key : text) {
            SendIt(PID, key);
        }
    }
    return 0;
}