Solved

Code example for SendInput in C++

Posted on 2004-09-05
5
28,207 Views
Last Modified: 2011-08-18
I'm currently trying  to develop an application that can generate keystrokes into another existing application. I've read around a bit and found that SendInput() is probably the function I need to be using, yet I'm having some problems getting it working. I'm trying and make it work at a basic level and just send one keystroke of character 'a' to an open text file in notepad. Can someone please tell me what I'm doing wrong in my code and how to fix please?

#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <iostream>

using namespace std;

void main()
{
char end;
HWND windowHandle = FindWindow(0, "test.txt - Notepad");
INPUT *key;
if(windowHandle == NULL)
 cout << "not found";
SetForegroundWindow(windowHandle);
Sleep(1000);

key = new INPUT;
key->type = INPUT_KEYBOARD;
key->ki.wVk = 41;
key->ki.dwFlags = 0;
key->ki.time = 0;
key->ki.wScan = 0;
key->ki.dwExtraInfo = 0;

SendInput(1,key,sizeof(key));

key->ki.dwExtraInfo = KEYEVENTF_KEYUP;

SendInput(1,key,sizeof(key));
cout << "key inputted";
cin >> end;
}

Thanks for your help.
0
Comment
Question by:daumont
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 11983447
You need to send key to Notepad edit area which is child of Notepad frame window, and not to Notepad frame itself. Use GetWindow to find child edit box from windowHandle parent. Use Spy++ to see windows hierarchy.
0
 

Author Comment

by:daumont
ID: 11983472
I have added and changed part of the code to:
HWND childWindow = GetWindow(windowHandle,GW_CHILD);
SetForeGroundWindow(childWindow);

I've also checked that childwindow handle matches the one in Spy++.
It still does not print anything into the notepad. Is using setforegroundwindow() the wrong way to set the focus on the application about to receive the input?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 11983634
SendInput(1,key,sizeof(key));

replace with:


SendInput(1,key,sizeof(INPUT));

sizeof(key) is 4 sins key is pointer.

0
 

Author Comment

by:daumont
ID: 11983642
Excellent work!. thanks
0
 

Expert Comment

by:DanielRD
ID: 26520358
Can I get the finished code for this example?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now