Solved

Code example for SendInput in C++

Posted on 2004-09-05
5
28,335 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

803 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