- 1 year ago
SendInput doesn't work after the Chorme preview window show
If the SendInput
function stops working after the Chrome preview window is shown, it could be due to the Chrome window receiving focus and capturing input events. The SendInput
function sends input events at the operating system level, and if another window, like the Chrome preview window, is in focus and intercepting input events, SendInput
may not have the desired effect.
To work around this issue, you can try the following options:
Bring the target window to the foreground: Before calling
SendInput
, use theSetForegroundWindow
function to bring the target window, to which you want to send input, to the foreground. This may help ensure that the target window receives the input events instead of the Chrome preview window.Use
SendMessage
instead ofSendInput
: Instead of usingSendInput
to simulate keyboard or mouse input, you can try using theSendMessage
function to send specific messages directly to the target window. For example, you can useSendMessage
with theWM_KEYDOWN
andWM_KEYUP
messages to simulate key presses and releases. This approach bypasses the need for system-level input events and sends the input directly to the window.
Here's an example of using SendMessage
to send a key press event:
cpp#include <Windows.h>
void SendKeyPress(HWND hwnd, WPARAM key)
{
SendMessage(hwnd, WM_KEYDOWN, key, 0);
SendMessage(hwnd, WM_KEYUP, key, 0);
}
int main()
{
HWND targetWindow = FindWindow(NULL, "Window Title"); // Replace "Window Title" with the actual title of the target window
// Ensure the target window is in focus
SetForegroundWindow(targetWindow);
// Simulate a key press event (e.g., 'A' key)
SendKeyPress(targetWindow, 'A');
return 0;
}
By using SendMessage
, you can directly send input events to the target window without relying on system-level input events.
Please note that sending input events to other applications can have security implications and may violate application usage policies. It's important to use such functionality responsibly and with proper authorization.