Solved

Code example for SendInput in C++

Posted on 2004-09-05
5
28,543 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 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