I want my program to interact with other programs, and i'm trying to find out how to send commands such as keystrokes to another program, similar to how a macro program works. Anyone know? thanks
Sending commands to other programs
Sending commands to other programs
I know this isn't an irrlicht question, but there's not a general programming thread in here and I don't know where else to ask this.
I want my program to interact with other programs, and i'm trying to find out how to send commands such as keystrokes to another program, similar to how a macro program works. Anyone know? thanks
I want my program to interact with other programs, and i'm trying to find out how to send commands such as keystrokes to another program, similar to how a macro program works. Anyone know? thanks
short answer: hooks
long answer: http://www.codeproject.com/system/hooksys.asp
good luck with long answer
long answer: http://www.codeproject.com/system/hooksys.asp
good luck with long answer
Do you 'know' the other window in some way?
You can obtain the window handle with this function
http://msdn.microsoft.com/library/en-us ... frame=true
Then you can send messages to it using this function
http://msdn.microsoft.com/library/en-us ... frame=true
You have to figure out what messages to send for yourself. They typically begin with WM_. WM_CHAR just sends a translated char.
The parameters for a WM_CHAR message are described here.
http://msdn.microsoft.com/library/en-us ... frame=true
Is this enough to solve your problem?
You can obtain the window handle with this function
http://msdn.microsoft.com/library/en-us ... frame=true
Then you can send messages to it using this function
http://msdn.microsoft.com/library/en-us ... frame=true
You have to figure out what messages to send for yourself. They typically begin with WM_. WM_CHAR just sends a translated char.
Code: Select all
HWND Window;
Window=FindWindow(NULL, "mIRC");
PostMessage(Window, WM_CHAR, 'g', 0);
http://msdn.microsoft.com/library/en-us ... frame=true
Is this enough to solve your problem?
If you don't have anything nice to say, don't say anything at all.
Luben wrote:Do you 'know' the other window in some way?
You can obtain the window handle with this function
http://msdn.microsoft.com/library/en-us ... frame=true
Then you can send messages to it using this function
http://msdn.microsoft.com/library/en-us ... frame=true
You have to figure out what messages to send for yourself. They typically begin with WM_. WM_CHAR just sends a translated char.
The parameters for a WM_CHAR message are described here.Code: Select all
HWND Window; Window=FindWindow(NULL, "mIRC"); PostMessage(Window, WM_CHAR, 'g', 0);
http://msdn.microsoft.com/library/en-us ... frame=true
Is this enough to solve your problem?
Nice. I was expecting it to be something like that but I didnt know which functions to use. I'll try it when I get back home.
I must be missing something because it doesnt seem to be sending anything. I tried the above code and I've tried things likeLuben wrote:Code: Select all
HWND Window; Window=FindWindow(NULL, "mIRC"); PostMessage(Window, WM_CHAR, 'g', 0);
PostMessage(HWND_BROADCAST, WM_CHAR, 'g', 5);
in case it wasnt getting the hwnd, I just broadcasted to all. To test it, I opened up notepad and tried getting my program to send it a bunch of g's, but nothing was happening. maybe it needs to be initialized somehow? any ideas?
Yeah, seems like the 'main' window doesn't forward those messages to the active window, sortof. You could try to check which window is active if you need dynamic input.
But if you need fixed input, first need to identify which child-window you are searching for. You can use this tool to identify window parameters etc.
http://www.bestsoftware4download.com/do ... mlbum.html
For notepad, we need to get the child window which is an edit-type window-class. Luckily it is also the first/only child of the notepad window, so we just have to do this.
But if you are just looking for ways to simulate input, independant of the target window, check out http://msdn2.microsoft.com/en-us/library/ms646310.aspx
Some programs, like most games, do not 'accept' input such as that, because they poll the keyboard themselves instead of waiting for a keyboard message.
But if you need fixed input, first need to identify which child-window you are searching for. You can use this tool to identify window parameters etc.
http://www.bestsoftware4download.com/do ... mlbum.html
For notepad, we need to get the child window which is an edit-type window-class. Luckily it is also the first/only child of the notepad window, so we just have to do this.
Code: Select all
HWND Window=NULL;
while(!Window)
Window=FindWindow("Notepad", NULL);
Sleep(25);
//This jsut happens to be the edit window in notepad.
Window=GetWindow(Window, GW_CHILD);
//Just for easy debugging
printf("%x\n",Window);
bool Run=true;
while(Run)
{
//Run until we fail, when the window is closed or something else happened
Run=false;
Run|=PostMessage(Window, WM_KEYDOWN, 'G', 0);
Run|=PostMessage(Window, WM_KEYUP, 'G', 0);
Sleep(1);
}
Some programs, like most games, do not 'accept' input such as that, because they poll the keyboard themselves instead of waiting for a keyboard message.
If you don't have anything nice to say, don't say anything at all.
I'm having trouble using INPUT (http://msdn2.microsoft.com/en-us/library/ms909626.aspx)
I found some examples using sendinput but I can't do anything with it because my compiler keeps saying INPUT is not defined even after I added #include <winuser.h> to my code.
I found some examples using sendinput but I can't do anything with it because my compiler keeps saying INPUT is not defined even after I added #include <winuser.h> to my code.