Google Answers Logo
View Question
 
Q: Using SendInput to send a number ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Using SendInput to send a number
Category: Computers > Programming
Asked by: jamest-ga
List Price: $50.00
Posted: 31 Jul 2006 12:41 PDT
Expires: 30 Aug 2006 12:41 PDT
Question ID: 751189
Can someone provide a simple C++ example of using SendInput to send the 
number 5 to Notepad in 20 lines of code or less?

Request for Question Clarification by keystroke-ga on 31 Jul 2006 13:11 PDT
I believe these two items will help you

http://msdn.microsoft.com/msdnmag/issues/05/01/CQA/

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21119534.html

#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(INPUT));
key->ki.dwExtraInfo = KEYEVENTF_KEYUP;
SendInput(1,key,sizeof(INPUT));
cout << "key inputted";
cin >> end;
}

Let me know if it points you in the right direction.

--Keystroke-ga

Request for Question Clarification by keystroke-ga on 31 Jul 2006 13:13 PDT
You will also need to have open a notepad file called "test.txt".
Otherwise this wont work.

--Keystroke-ga

Clarification of Question by jamest-ga on 31 Jul 2006 13:38 PDT
The second link (experts-exchange.com) looks like it should work, but
the person posting states that it does not work: "Can someone please
tell me what I'm doing wrong in my code and how to fix please?"

Request for Question Clarification by keystroke-ga on 31 Jul 2006 15:04 PDT
The answer is at the bottom of the page

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

replace with:


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

sizeof(key) is 4 sins key is pointer."

I already merged his code in with the original posters. I do not have
a c++ compiler on this machine so I cannot test it myself, also my C++
knowledge is a little rusty :)

--Keystroke-ga

Clarification of Question by jamest-ga on 31 Jul 2006 18:03 PDT
Great!  This is the first question I have ever posted.  How do I accept your answer?
Answer  
Subject: Re: Using SendInput to send a number
Answered By: keystroke-ga on 01 Aug 2006 03:20 PDT
Rated:5 out of 5 stars
 
The good thing with google answers is that you don't have to accept
the answer, once we have worked out the best answer for you (unless it
is obvious or the researcher is 100% sure of answering correctly) then
generally most researchers answer the question in the clarification
section and when the answer is accepted they post it in the Answer
box. Once that is done the answer is automatically accepted and
payment is sent. I hope that answers your question, I'll give you that
one for free ;)

I believe these two items will help you

http://msdn.microsoft.com/msdnmag/issues/05/01/CQA/

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21119534.html

#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(INPUT));
key->ki.dwExtraInfo = KEYEVENTF_KEYUP;
SendInput(1,key,sizeof(INPUT));
cout << "key inputted";
cin >> end;
}

Let me know if it points you in the right direction.

--Keystroke-ga
jamest-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy