我正在向应用程序发送CTRL + A和CTRL + C(显然是为了复制内容)。 为此,我写了一些看似没问题的C ++代码。
事实上,我在间谍++上看到,我的代码生成并发送到应用程序的消息与应用程序在输入CTRL + A和CTRL + C时手动接收的消息完全相同(期望frepeat值)键盘...除了使用我的代码,应用程序最后收到两条额外的WM_CHAR消息“A”和“B”。
因为我不发送这些WM_CHAR消息,但只发送WM_KEYDOWN和WM_KEYUP,我有点疑惑。顺便说一句,没有选择任何内容,也没有任何内容被复制(即使之前被选中)
这是我的C ++代码:
HWND hTargetWindow=(HWND) 0x280908;
LPARAM lparam1 = 0x00000001 | (LPARAM)(0x1D << 16);
LPARAM lparam2 = 0x00000001 | (LPARAM)(0x1E << 16);
LPARAM lparam3 = 0x00000001 | (LPARAM)(0x2E << 16);
LPARAM lparam1_ = lparam1 | (LPARAM)(0x1 << 31);
LPARAM lparam2_ = lparam2 | (LPARAM)(0x1 << 31);
LPARAM lparam3_ = lparam3 | (LPARAM)(0x1 << 31);
PostMessage(hTargetWindow, WM_KEYDOWN, VK_CONTROL, lparam1);
PostMessage(hTargetWindow, WM_KEYDOWN, VK_A, lparam2);
PostMessage(hTargetWindow, WM_KEYUP, VK_CONTROL, lparam1_);
PostMessage(hTargetWindow, WM_KEYUP, VK_A, lparam2_);
PostMessage(hTargetWindow, WM_KEYDOWN, VK_CONTROL, lparam1);
PostMessage(hTargetWindow, WM_KEYDOWN, VK_C, lparam3);
PostMessage(hTargetWindow, WM_KEYUP, VK_C, lparam3_);
PostMessage(hTargetWindow, WM_KEYUP, VK_CONTROL, lparam1_);
分别在和
a)手动输入CTRL + A CTRL + C时收到的消息 b)这里是当我的C +代码
发送CTRL + A CTRL + C时收到的消息
对于KEYUP事件我会把frepeat放到1但我怀疑这会改变什么,所以无论如何我都会发布这个问题。
那么为什么我的代码会发送这两条额外的消息?
提前感谢任何提示。
在下午7:09:05(GMT + 2:00)添加:
CTRL + A的KEYUP和KEYDOWN是相反的(CTRL + C序列是相同的),但这是因为我也尝试过这个来解决问题。我也尝试了很多次正确的组合。
这是spy ++,当keydown和keyup dequence完全相同时,不会改变任何东西:
答案 0 :(得分:0)
您在错误的顺序中执行操作,在WM_KEYUP
VK_A
之前执行WM_KEYUP
VK_CONTROL
。 C
也是如此。反转那些,应该没问题。
答案 1 :(得分:0)
// Send [CTRL-A] to select the entire text in a Notepad window, even if Notepad is out of focus,
// without bringing the Notepad window into focus.
// Works with Notepad, but not with Command Prompt window.
BYTE gucKeybStateCur [256] = {'\0'};
// hwN = Find Notepad HWND
AttachThreadInput (GetWindowThreadProcessId (hwN, NULL), GetCurrentThreadId (), TRUE);
GetKeyboardState ((PBYTE) &gucKeybStateCur);
gucKeybStateCur [VK_CONTROL] |= 0x80;
SetKeyboardState ((LPBYTE) &gucKeybStateCur);
PostMessage (hwN, WM_KEYDOWN, (WPARAM) 0x00000041, (LPARAM) 0x001E0001);
PostMessage (hwN, WM_KEYUP, (WPARAM) 0x00000041, (LPARAM) 0xC01E0001);
GetKeyboardState ((PBYTE) &gucKeybStateCur);
gucKeybStateCur [VK_CONTROL] &= 0x0F;
SetKeyboardState ((LPBYTE) &gucKeybStateCur);
AttachThreadInput (GetWindowThreadProcessId (hwN, NULL), GetCurrentThreadId (), FALSE);
// Send [CTRL-C] to interrupt a batch file running in a Command Prompt window, even if the Command Prompt window is not visible,
// without bringing the Command Prompt window into focus.
// [CTRL-C] will have an effect on the batch file, but not on the Command Prompt window itself -- in other words,
// [CTRL-C] will not have the same visible effect on a Command Prompt window that isn't running a batch file at the moment
// as bringing a Command Prompt window that isn't running a batch file into focus and pressing [CTRL-C] on the keyboard.
ulong ulProcessId = 0UL;
// hwC = Find Command Prompt window HWND
GetWindowThreadProcessId (hwC, (LPDWORD) &ulProcessId);
AttachConsole ((DWORD) ulProcessId);
SetConsoleCtrlHandler (NULL, TRUE);
GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0UL);
SetConsoleCtrlHandler (NULL, FALSE);
FreeConsole ();