为什么在向应用程序发送击键时会收到额外的消息? [英] Why Am I getting extra messages when sending keystroke to an application?

查看:25
本文介绍了为什么在向应用程序发送击键时会收到额外的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 CTRL+A 和 CTRL+C 发送到应用程序(为了明显地复制内容).为此,我编写了一些似乎没问题的 C++ 代码.

I am sending CTRL+A and CTRL+C to an application ( in order to copy the content obviously ). For this I wrote some C++ code which seems to be ok.

确实,我在 spy++ 上看到我的代码生成并发送到应用程序的消息与输入 CTRL+A 和 CTRL+C 时应用程序收到的消息完全相同(期望 frepeat 值),手动在键盘...除了使用我的代码,应用程序最后会收到两个额外的 WM_CHAR 消息,分别是A"和B".

Indeed I see on spy++ that the messages produced and sent to the application by my code are exactly the same ( expect frepeat value ) than the messages received by the application when CTRL+A and CTRL+C are inputed ,manually on the keyboard... except that with my code, the application receive at the end two extra WM_CHAR messages for 'A' and 'B'.

因为我不发送这些 WM_CHAR 消息,而只发送 WM_KEYDOWN 和 WM_KEYUP,所以我有点困惑.顺便说一句,什么都没有选择,什么也没有复制(即使之前选择了)

since I do not send these WM_CHAR messages but only WM_KEYDOWN and WM_KEYUP I am slightly puzzled. Incidentally nothing is selected and nothing is copied ( even if selected before )

这是我的 C++ 代码:

here is my C++ code:

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 时收到的消息

a) the messages received when CTRL+A CTRL+C are inputed manually b) here the messages received when when CTRL+A CTRL+C are sent by my C+ code

我会将 KEYUP 事件的 frepeat 设置为 1,但我怀疑这会改变任何事情,所以我还是发布了这个问题.

I will put frepeat to 1 for KEYUP events but I doubt this will change anything so I post the question anyway.

那为什么我的代码会发送这两条额外的消息?

提前感谢您的任何提示.

thanks in advance for any hint.

在晚上 7:09:05(格林威治标准时间 + 2:00)添加:

CTRL+A 的 KEYUP 和 KEYDOWN 颠倒了(CTRL+C 顺序相同),但这是因为我也尝试过此方法来解决问题.我也试过很多次正确的组合.

the KEYUP and KEYDOWN for CTRL+A are reversed ( CTRL+C sequence is the same ) but this is because I have also tried this to solve the problem. I have also tried many times the right combination.

当 keydown 和 keyup 序列完全相同时,这是 spy++,不会改变任何东西:

this is spy++ when the keydown and keyup dequence is exactly the same , that does not change anything:

推荐答案

// 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 ();

这篇关于为什么在向应用程序发送击键时会收到额外的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆