Sending text/keystrokes to unselected window?
Asked Answered
L

2

5

Is there a way to send keystrokes to a window that is not currently selected in C++? For example, if I have a notepad window minimized and want some text to be typed in it without bringing the window to the front.

I'm using Windows 7 64-bit.

Longboat answered 5/3, 2011 at 18:9 Comment(1)
I added the "winapi" tag. C++ does not have windows; Windows has windows, and you can use the Windows API to manipulate them.Gaylegayleen
R
6

Faking input is rather hard to achieve, in full generality, without using SendInput().

Yes you can try PostMessage(), but the answer from eznme is misleading at best when it talks about SendMessage. As I, and others, seem to say many times a day here, input is posted to the message queue rather than sent to a window handle.

All that said, if you don't want to give the Notepad window input focus then it's going to be hard to get the text in there by faking. The very simple alternative that works better and is easier to use, is to find the window handle of the Notepad EDIT window and use WM_GETTEXT and WM_SETTEXT, for example, to modify its contents directly.

In fact there is an enormous multitude of functionality available once your have this window handle at your mercy!

Retuse answered 6/3, 2011 at 9:12 Comment(0)
B
4

Absolutely: Check out PostMessage() and SendMessage(), they are part of the Windows API:

http://msdn.microsoft.com/en-us/library/ms644944%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms644950%28VS.85%29.aspx

Specifically you probably want to send WM_KEYUP

http://msdn.microsoft.com/en-us/library/ms646281%28VS.85%29.aspx

Besides answered 5/3, 2011 at 18:17 Comment(1)
-1 SendMessage is utterly inappropriate, PostMessage less than ideal.Retuse

© 2022 - 2024 — McMap. All rights reserved.