Sending text/keystrokes to unselected window? - c++

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.

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!

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

Related

Send keybinds to a non-active window

So this has been bothering me for quite a while now.
I have a program for sending keys to a window (user may select processes from a CComboBox, etc.), and I'm now trying for quite a while to figure out how it would be possible to send key-binds to a window (background !).
I know about this keybd_event function and the SendInput function. I use neither of them in my current code, as they require the window to be in the foreground.
Short said, my program can send keys and text to any (valid) window.
PostMessage(m_hwndEx, WM_CHAR, nKey, 0x00140001);
I have tried various solutions with SendMessage and PostMessage, but nothing seems to work.
Assume the program sends CTRL+SHIFT+F (as example). How would this be possible ? I would appreciate some pointer function(s) or even an external library if the 'basic' functions do not work for this matter.
I do not want the window to be in the foreground.

Most suitable way to read keyboard input in C++

I'm trying to write a Keyboard class that can read in the keyboard buttons. I have looked at this link - http://www.daniweb.com/software-development/cpp/code/216732/reading-scan-codes-from-the-keyboard But as stated on there, it is not very accurate for all computers (I don't know if this is even true). Therefore, my question is whats the best method in implementing my keyboard class? This will be used for Windows
Many thanks
There are three ways to read keyboard input:
By reading input from a console window as described in your link. It's true that it's hard to get this to work correctly, for starters because it's reading ANSI characters and not Unicode characters, but there are other issues. Console input/output is kind of obscure, as is the documentation for it
By handling UI events associated with a normal window. In this case you would handle the WM_KEYDOWN message in a window procedure
By going deep into the Win32 API with functions like SetWindowsHookEx. In this case you don't even need a window (normal or console), and you can read keystrokes pressed in any application or in the desktop
It's hard to suggest which one to use without knowing how you intend to use this Keyboard class.

How do I manipulate input from the keyboard in Win32?

Or to clarify this question, how can I make Windows think I hit a key, when I really didn't? I know I could possibly use SendMessage and specify the input there, but then wouldn't only my application receive it? I'd like control to the extent of all applications receiving the "fake" input. Any advice?
What you describe, faking input, is implemented by the SendInput function. Input goes to the thread which has input focus.
You can SendMessage to whatever window you want, even on other processes. You can even use HWND_BROADCAST to send it to every to-level window on the system. But is that what you really want? If you're only interested in a specific program, you can get its window's handle using FindWindow, and then send the message only to that window.
Note that if all you want to do is a simple keystrokes injection into another process, then SendInput is indeed the way to go. If you'd like to send some global keyboard shortcut it doesn't matter who has the focus. If you'd like to send the same input to more than one window using SendInput, you'll have to loop over the list of windows, and for each window first set the focus and then send the input.

C++ -- Win32 API, GUI stuff

I've been messing with Win32 API for abit, and I've got a question regarding the GUI functions.
How does one handle user input that is not managed through popup windows? I've been reading http://www.winprog.org/ but right when the interesting features comes -- lesson9 -- it becomes more abstract and I'm not sure how to do it.
Basically what I am after is the user to write input in two windows and then press a button to send a message that the content of the input is to be processed.
I think the input windows would be some EDIT-class windows and the input BUTTON-class but that's about it.
Any ideas? I'm sure it's simple, it's just makes me want to rip my hair of in native code :p
Cheers
You're correct, you want and EDIT control which is more commonly known as a TextBox and a BUTTON class which is a command button.
To get the the input the Button will send a WM_COMMAND message to its parent window with a BN_CLICKED in the wParam high word. You can identify the particular button from the hWnd you get in that message.
After that you'll need to post a WM_GETTEXT to the edit control to retrieve the users input.
This is all from memory so I highly recommend looking up the msdn pages before you code.
I'm not sure I follow 100%. Yes, you would use EDIT and BUTTON-class controls for that. Where are you getting stuck?

How to send text to an application?

I am trying to make an application, which reads the data from a serial port (on the serial port there is a barcode scanner plugged in), and then forwards it to an application. I can read data from serial port now, but i don't know, how to forward the read text, to an application, for example notepad. I tried to use SendMessage() API but it didn't succeed. Maybe i did something wrong.
Could someone help me, and maybe show some example?
Thanks,
kampi
Sounds like you're looking for keybd_event or the newer SendInput. It allows you to simulate keyboard input.
HWND hwnd = FindWindow(NULL, L"Untitled - Notepad");
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)L"Hello!");
This will set the Notepad's title bar text to Hello. Of course, you can elaborate a bit to find Notepad's textbox control, or to find your own control in an application, or to find the control that has focus in the active foreground window (see GetForegroundWindow), but the idea is that once you have a hwnd of the window/control you want to set text, the above code should work.
If you want to send it to the Notepad, then it would be easier to save the text into a temporary file and then open it with the Notepad. From a Windows application this could be done using CreateProcess.
On the other hand, if you in control of how the receiver application is working, you could use different approaches, such as: pipes, window messages, shared memory and some others. This is a good place to start.
If you mean another application, you should use one of IPC methods.
The simplest method should be named pipes.