char WM_KEYDOWN在硬编码时显示为数字而没有大写字母
char WM_KEYDOWN showing up as number and no capitol letters when hard coded
我正在写一个可以发送按键到另一个应用程序的应用程序。当我硬编码我想要发送的文本时,我有一些它在工作。然而,当我使用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
。
- 命令行参数不产生提供的实际字符。
- 即使字符串是硬编码,输出到另一个cmd.exe窗口也不是大写字母。
提前感谢!!
正如Raymond Chen博客所说:
你不能用PostMessage模拟键盘输入。
即使你可以可靠地做到这一点,你的代码也不是:
-
考虑目标进程有多个窗口的可能性。甚至当你正在发送击键时,它可能会在窗口之间切换。你正在发送你的关键消息到进程中的随机窗口,甚至只发送到顶级窗口,而不是发送时具有键盘输入的任何特定子控制窗口。
-
发送任何
WM_KEYUP
消息,只能发送WM_KEYDOWN
消息 -
发送虚拟密钥码。
WM_KEYDOWN
/UP
不操作字符,而是操作键码和扫描码。另一方面,WM_CHAR
对字符进行操作。 -
考虑小写和大写,这需要模拟在发送大写字符时按住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, >i))
{
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;
}
- 如何在 c++ 中将所有大写字母转换为字符串中的小写字母,反之亦然?
- 大写字母
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- cin.fail 在输入数字和字母时不起作用
- C++ - 我在输出时遇到问题,因为我正在收到数字和字母
- 字符数组[]中的小写大写字母C++通过指针
- 如何在德语大写字母 Ä, Ö, Ü, ẞ 上正确应用 C++
- 删除 C 字符串中的大写字母
- 如何检查包含 utf8 文本的 std::string 在 Windows 中是否以大写字母开头?
- 尝试创建一个评估字符的bool函数,如果字母数字为字母,则返回true
- 正则表达式表示非连续大写字母和小写字母
- 生成从 1 到 1000 的卡拉奇特系列的程序,其中包括数字和字母
- C++ 如何从输入字母或数字输出字母或数字
- 如何用C 中的数字替换字母
- 如何在c++中为数据中的特定数字分配字母
- 如何从用户那里获得输入字符串而不是示例,然后计算空格、标点符号、数字和字母.C++
- C++函数来检查 char 是否是大写字母,并且不计算给定字符串中的大写字母
- 将大写字母(从 cin 读取)更改为小写
- 如何计算字符串中大写字母、小写字母和数字的数量
- char WM_KEYDOWN在硬编码时显示为数字而没有大写字母