Solved

Code example for SendInput in C++

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 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