forced key presses

Started by
17 comments, last by Drew_Benton 19 years, 3 months ago
how would you make a program send a keypress event without having the user actually press the key so that the computer would THINK the person has pressed the key im doing this in SDL so any relation to that API would be helpful thanks [smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
You will need to use SDL_PushEvent.

SDL_Event event;event.key.keysym.sym = SDLK_RETURN; // Or your key hereevent.type = SDL_KEYDOWN; // Or keyup if you wanted to make the key releaseSDL_PushEvent(&event);


- Drew

[edit] Fixed hyperlink.[/edit]
mouse_event() may help.

SendInput() may also be of service.


[edit: Beaten to it by a better answer. booo.]
will these key presses appy to external programs (if not how would i do that)
like say i opened up microsoft word and wanted to write a program that types my name then quits (stupid i know just an exapmple k) how would i get those key presses to effect my program? would i have to use Win32 programming ?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
[sigh] there has to be some one here that has some input
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
SDL doesn't provide any functionality to inject input into other programs. That would be a function of the operating system you're using. I expect Win32 will cover it, yes.
Quote:Original post by raptorstrike
[sigh] there has to be some one here that has some input


Ok this is what you need to do:

First: Obtain a handle to the window you want to send input to. To do this you need to know the class name and program window title. This information can be found with Spy++ or someother window utility. For this little tutorial I will use Notepad. Go ahead and open it up. After using spy++ the window title is "Untitled - Notepad" and the class name is "Notepad". Now in our little program we will first obtain the handle to the program using FindWindow:

HWND notepad = FindWindow("Notepad","Untitled - Notepad");if( !notepad ){    cout  << "Cannot find window!";    return 0;}


Now that we have the window's handle, we can now send it a message. THis message will do whatevr you want it to - quit, minimize, send in input, etc.. Since you want to send in your name, we must send in a message with the string. Just to show how this will work at first, add in this line:

SendMessage(notepad,WM_CLOSE,0,0);


When you execute, you will notice that NotePad will close. Go ahead and open it up again. Now here is a quick little Win32 history. Any program is made up of controls. In notepad there are 3 main ones - the window itself, the edit box you can type in, and the menu. We cannot simply send key strokes to the program, because it does not have focus. Instead we must send messages to the edit control. To do this, we will use FindWindowEx to obtain the handle to the control. We must also know the class and caption of the edit control. We can use spy++ to get this as well. I find that the class name is "Edit" and the caption is "" - since there is not text in it yet:

HWND control = ::FindWindowEx( notepad, 0, "Edit", "" );if( !control ){    cout  << "Cannot find control!";    return 0;}


Now that we have the control handle we can send it some text! To do this we will use the send message with a special message of sending a string in:

TCHAR buf[512];memset(buf,0,512);sprintf(buf, "Raptorstrike!!");SendMessage(control, WM_SETTEXT, sizeof(buf)/sizeof(TCHAR), (LPARAM)(void*)buf);


Now when you run the program - you will see the string "Raptorstrike!!" pasted into the window of notepad!

Here is the complete source:

#include <windows.h>#include <iostream.h>#include <stdio.h>int main(int argc, char* argv[]){	HWND notepad = FindWindow("Notepad","Untitled - Notepad");	if( !notepad )	{		cout  << "Cannot find window!";		return 0;	}	HWND control = ::FindWindowEx( notepad, 0, "Edit", "" );	if( !control )	{		cout  << "Cannot find control!";		return 0;	}	TCHAR buf[512];	memset(buf,0,512);	sprintf(buf, "Raptorstrike!!");	SendMessage(control, WM_SETTEXT, sizeof(buf)/sizeof(TCHAR), (LPARAM)(void*)buf);	return 0;}


I do not have access to my ftp currently so I cannot upload a project for you, but that program should compile as is with no modifications. This is just one way of doing this technique. THere are many others that can accompish this and more. I hope this helps you some! Feel free to ask anything you have regarding to what I have posted. [smile]

- Drew
good news: i think i understand whats going on and your exapmple works great,
bad news: i can ONLY do Notepad windows, if i even try and find any other kind of window it cant this code does nothing because it cant find the window, were is the place were i can find the name of the window?

#include <windows.h>
#include <iostream.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
HWND notepad = FindWindow("Notepad","x - Notepad");
if( !notepad )
{
cout << "Cannot find window!";
return 0;
}
HWND Word = FindWindow("Microsoft Word","Document1 - Microsoft Word");
if( !Word )
{
cout << "Cannot find window!";
return 0;
}
HWND control = ::FindWindowEx( notepad, 0, "Edit", "" );
if( !control )
{
cout << "Cannot find control!";
return 0;
}

TCHAR buf[512];
memset(buf,0,512);
sprintf(buf, "Raptorstrike!!");

SendMessage(control, WM_SETTEXT, sizeof(buf)/sizeof(TCHAR), (LPARAM)(void*)buf);

return 0;
}


this is identicle to your code except i also added a search for a basic word decument and even though its open you cant find it

thanks for the reply[smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
No problem! I have Word 2003 - the class name is not the program's title! For my word it is "OpusApp". You will need to use Spy++ to get the correct class. Hit ctrl+F to bring up the find window in spy, then select hide spy++ check box, and finally drag the target over your word application. As for the edit box class name - mine is "_WwG". If you cannot get it to work, I will get back to you on Word with a modified project. Good luck!

- Drew
actaully i dont have MVC++, is there any other way that i could get prosses names? would (ctrl + alt + delete) then right click on app then "goto prosses" give me the correct name?

thanks again for your help
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie

This topic is closed to new replies.

Advertisement