Solved

Code example for SendInput in C++

Posted on 2004-09-05
5
28,386 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
is twain_32.dll cmpatible with windows 10 ? 10 186
gdb doesn't stop on breakpoint 2 95
show out valin of json for debugging in visaul c++ 1 41
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

861 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