Switching windows and sending keystrokes

I've been looking for a while on this and can't find anything that I can understand (some on here). People direct others to websites where it is rather confusing.

1
2
3
4
5
HWND windowHanel = FindWindow(0, "Google - Mozilla Firefox");
if (windowHandle == NULL)
	cout << "not found";
SetForegroundWindow(windowHandle);
Sleep(1000);


Is what I have so far, to, as I understand it, switch the focus of windows to a mozilla window. Now what is the command to send key strokes to the other window? All the examples I find have "void" and like 15 different lines of code w/o explanation.

I found the Sendinput but no real explanation for someone (with my level of understanding) to be able to apply it to a program.

I would think it would be as simple as something like

1
2
Sendinput(windowhandle, KEY_P_Press)
Sendinput(windowhandle, KEY_P_Depress)

and that that, alone, would just send the momentary pressing of the "P" key (obviously toggled by caps as relevant). I read some things that imply that may be the case, but all the other examples either use terms and commands I don't understand or have like 30 lines of code for one keypress.

I'm essentially wanting to have a program behave, somewhat, like a macro program, as the variables I need the macro behavours (just keystrokes) to be based upon, are more easily found in the C++ program than using a normal macro program to do it instead.
SendInput() is the right function to use. Since it is used to simulate mouse and keyboard input, it is not as simple to use as you wish. It is the way it is and you must deal with that, and most likely this function will change to accommodate future input sources. I wouldn't be surprised if Windows 8 added more of these, like accelerometer input or something like that.

So back to SendInput(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx . This is the official documentation. It takes 3 parameters that are explained there. In your example of simulating the key press of the P key, you have to create an array of 1 INPUT structure, or simply a single INPUT structure. Set the type member of the INPUT structure to INPUT_KEYBOARD and then proceed to fill in the ki member.

The SendInput() function actually simplifies the process of simulating a single keystroke. If you read the explanation of the KEYBDINPUT structure (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271(v=vs.85).aspx ), you'll see that all you need to do is set the wScan member to L'P' and specify the flags KEYEVENTF_KEYUP and KEYEVENTF_UNICODE. The function will then send the appropriate WM_KEYDOWN and WM_KEYUP messages to the foreground window.

Is this too much hassle for a single keystroke? Probably, but it is the way it is.
Fair enough. I tried using the Keyb_input command but then my compiler complains

"error LNK2019: unresolved external symbol __imp__keybd_event@16 referenced in function _main"
"fatal error LNK1120: 1 unresolved externals"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <tchar.h>
#include <urlmon.h>
#include <dos.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
#pragma comment(lib, "urlmon.lib")

int main()
{
	system("c:\\windows\\notepad.exe");
	Sleep(250);
		
	keybd_event('a',0x45,0,0);
	keybd_event('a',0x45,2,0);

	
	return 0;
}


is my full program (yea, some of the includes I don't need, but I'm getting used to adding the same header files to all my programs to accommodate whether I recall which one is used for what and whether I suddenly add some commands that requires another one). But this program gives that error, so, once again, I'm left confused. Other variations I've tried have failed to even compile as well.
I'm at the point where I'm thinking of using a macro program to save all the web pages I need saved through mozilla. Seems it may be the easiest method that I know how to do 8(
Last edited on
LOL, all those #includes for that program? You only need windows.h and the one where system() is (don't know which as I never use system() ). You also don't need using namespace std; and you don't need that #pragma including the urlmon.lib file.

What compiler are you using? According to MSDN Online (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx ), you need to link against user32.lib. In Visual Studio you shouldn't need to specify this lib file for linking.

In any case, you could add #pragma comment(lib, "user32.lib") . It should work OK.
Thank you, turns out that user32.lib was what was needed, and I"m using MV C++ 2010 Express.

Here's what's still confusing to me though.

Supposedly my code "keybd_event('a', 0x45,0,0); supposedly will hit the key "a". But the 2nd part, the "0x45" is what is supposed to identify what key is pressed, so the first 'a', I would think, would be unecessary. But, according to that site you linked (which I had been reading) states that "0x45" is actually "e", so how does that work out? Also it states the third part where I have "0" and then "2" (to depress the key) it doesn't specify that on that site, but I read that on another site. Those are the things I meant originally when referencing that the other sites are "confusing"; I don't see the continuity to the point where I can comprehend and apply it.

One more thing, when running that program, it just sits there (even when I removed the sleep). The console doesn't close and it doesn't type anything into notepad (though it does open it up).
No, the first parameter receives the key code. The 0x45 you see is in the SECOND parameter, which is the hardware scan code. What is a hardware scan code? I don't know. Look it up. The third one is a flag value. A value of 2 (never use "magic numbers" like you did, instead use the named constant KEYEVENTF_KEYUP) indicates that you are simulating the key up.

Your program must not freeze at any point. You should be able to verify this by setting up a breakpoint in line 23. The breakpoint should be hit 100% of the times. Also add a larger sleep time, like 2000 just to make sure, and add another Sleep() call after the last keybd_event() call to ensure it is Notepad the one that receives the keyboard input before Visual Studio pops up because of the breakpoint in line 23.
it freezes at the line

system("c:\\windows\\notepad.exe");

I confirmed this by putting a cout immediately before and after.
Well, then it seems that system() synchronizes. Meaning: It will not continue until notepad.exe is finished (meaning it is closed). Use CreateProcess().
Try system("start c:\\windows\\notepad.exe");
Thank you, start worked! But now another problem presents itself. The keystrokes aren't being sent as they supposedly should be. With my program above it doesn't send the letter "a", but instead sends a "1". Not that I would *really* mind having to send entire URLs to a browser one character at a time via this method, the resulting characters don't seem to coincide with the tables depicting the characters for the codes in the command keybd_event.

So now, I'm wondering, how do I go about sending just a set of text, or string to the other program?

http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx

Has info on "SendKeys" and I got the header file and all, but upon compiling, there was some sort of error it seems other people have as well. So, is the keybd_event the only realistic way to send key strokes to another program (and only one at a time) or is there a better way perhaps similar to cout? like Sendtootherprogram << "Hello world";
Topic archived. No new replies allowed.