Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending text/keystrokes to unselected window?

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.

like image 254
Tim Avatar asked Mar 05 '11 18:03

Tim


2 Answers

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!

like image 67
David Heffernan Avatar answered Nov 03 '22 06:11

David Heffernan


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

like image 20
Bernd Elkemann Avatar answered Nov 03 '22 06:11

Bernd Elkemann