Marco Sanfilippo Marco Sanfilippo - 3 months ago 26
C++ Question

Send Keystroke to a generic window c++ with SendInput

I have a C++ console application that would to send some Keystroke to one generic window...
I'm trying to find ALL OPEN WINDOW WITH MESSAGE QUEUE (for input) with

EnumWindows()
and my callback store some information about, it has
GetWindowText()
,
GetWindowThreadProcessId()
and
PostThreadMessage()
(to check if has Message queue, with
WM_NULL
message) and also its
HWND hWnd
... [code below]

Hints: Is this correct or I should use also
EnumChildWindows()
?

continue...

I wish to send keystroke for example
ALT+F4
or
CTRL+T
etc. to one of the windows ...

Hints: Which is the best way to do this?
SendInput()
or
PostThreadMessage()
?

[HALF auto-reply]: I'm currently trying
SendInput()
, sending a group of
INPUT
using the translation of Scan Code in VirtualKey with
myVKey = MapVirtualKeyEx(..., MAPVK_VSC_TO_VK, ...)
, for example
ALT
and
F4
is
56
and
62
as
int
type, in
HEX
is
38
and
3E
... I make input with
dwFlags = 0
and after, in reverse order, with
dwFlags = KEYEVENTF_KEYUP

(I need to handle only a group of command, no other like single char etc..);

I think VKey is better than Hardware Scan Code (right?)

Third Question: Before using
SendInput()
, how I should bring the window?

Let's supposing I have all info about the window...

What and in which order I must use
SetFocus()
,
SetActiveWindow()
and / or
SetForegroundWindow()
? [all are commented]

a bit of code:

bool _res = false;
_res = AttachThreadInput(GetCurrentThreadId(), targetThreadID, TRUE);
if (!_res) {
cout << "Error: " << GetLastError() << endl;
}
else {
//SetFocus(focusHandler);
//SetActiveWindow(focusHandler);
//SetForegroundWindow(focusHandler);
enter code here
if (!prevH) {
cout << "Error: " << GetLastError() << endl;
}
else {
UINT res = SendInput(nTotVKey, numInputVect, sizeof(INPUT));
if (res != nTotVKey) {
cout << "Error: " << GetLastError() << endl;
}
else {
_res = AttachThreadInput(GetCurrentThreadId(), targetThreadID, FALSE);
if (!_res) {
cout << "Error: " << GetLastError() << endl;
}
}
}


OFF:
Ok, I read that
PostThreadMessage()
is better than
SendInput()
but I don't know how to translate ScanCode to some WM_MESSAGE (is another process that send the ScanCode)...

Any help would be appreciated. Thanks..

EDIT: REAL SCENARIO Is a C/S application in which client says "I do a CTRL+W to the selected window" and Imagine a C# client that send to the server a "CTRL+W" message for Google Chrome (for example) that the server should send to its thread input queue... I need to put in foregound "Google Chrome" to send the input "CTRL+W" ..don't worry about any race condition or other.. If needed the client will send again the command...

Answer

Thanks to all. I've found my answers on StackOverflow's past questions.

1: Is this correct or I should use also EnumChildWindows() ?

A: NO I don't need it because almost of Child will receive the input from parent.

2: Which is the best way to do this? SendInput() or PostThreadMessage() ?

A: SendInput() with Virtual Key.

3: What and in which order I must use SetFocus(), SetActiveWindow() and / or SetForegroundWindow()?

A: Is enough SetFocus().