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.
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'.
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 )
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_);
and here respectively
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
I will put frepeat to 1 for KEYUP events but I doubt this will change anything so I post the question anyway.
so why are these two extra messages sent by my code ?
thanks in advance for any hint.
added 7:09:05 p.m.(GMT + 2:00):
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.
this is spy++ when the keydown and keyup dequence is exactly the same , that does not change anything:
You're doing things in the wrong order, doing the WM_KEYUP
on VK_A
before you do the WM_KEYUP
on VK_CONTROL
. Same for the C
. Reverse those and it should be fine.
// 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 ();
User contributions licensed under CC BY-SA 3.0