How to send keystrokes to a game

01/09/2012 00:55 DarkTwilight#1
Dear reader,

I am recently starting to learn C++.
I have the following problem:

I build this GUI program which should repeat my shouts
by a simple method.

Send a "space", then "up key", then "enter key" command.
Only this is, that it will not respons if the window is not focussed.

I want to be able to do these things even though when the window
is minimized for example. Could anyone tell me how to do it, or write
(link) to a tutorial so I can learn how to do it?

I think I have to hook to Final Fantasy XIV or something? but I have
no experience with hooks and that kind of stuff :(

Best regards
01/09/2012 07:54 MoepMeep#2
Send/PostMessage.
01/09/2012 15:29 DarkTwilight#3
Hey MoepMeep, thanks for your reply!

Yes I tried Send/Post message but those do nothing for me.
Nothing happens at all.

My current code:

Quote:
// bring the window to the front
HWND GameWindow = FindWindow(0, L"Untitled- Notepad");
SetForegroundWindow(GameWindow);

// execute the loop
for( int i = 0; i < amount; i++ ){
// not the last loop so add a pause at the end
if( i < (amount-1))
{
PostMessage(GameWindow, WM_KEYDOWN, 'G', 0);
PostMessage(GameWindow, WM_KEYUP, 'G', 0);
Sleep(2000);
}
// last loop so dont add a pause at the end
else
{
PostMessage(GameWindow, WM_KEYDOWN, 'G', 0);
PostMessage(GameWindow, WM_KEYUP, 'G', 0);
}
}
01/09/2012 15:32 MoepMeep#4
use GetLastError();
01/09/2012 15:40 DarkTwilight#5
Thanks I will give it a try right away :D
01/09/2012 15:45 Dr. Coxxy#6
the game most likely will ignore all keystrokes send to it while minimized, or doesnt even use the windows message pump to get keystrokes.
if it doesnt use the message pump for keystrokes you have to hook the method its using to check for keystates (will most likely be DirectInput) and hook it.
Tut: [Only registered and activated users can see links. Click Here To Register...] (German though, but full code sample for a directinput hook.)
if it doesnt react to keystrokes sent to it while minimized you have to reverse and find out how it checks whether its minimized/maximized and accepts keystrokes.

both are a lot of work - have fun.

edit:
btw. instead of saying "thanks" in your posts to moepmoep you can press that little "*THANKS" button right down at the post to increase our Thanks Counter and to maximize our E-Penis-Size.
Thanks in advance ;)
01/09/2012 15:48 DarkTwilight#7
yeah thats what I was thinking about lol. Hooking, but I have totally no knowledge on how to do that or how that works haha.

I know that someone made a bot called OttoBot which does that though, he sends keys and everything to the game minimized or not, it does not matter lol. Thats what I wanted to learn, how to send keys even if something is minimized haha.
01/09/2012 15:53 Dr. Coxxy#8
editied smth in my post look up.
01/09/2012 16:30 DarkTwilight#9
awesome thanks Dr. Coxxy!!!

and your E-Penis-Size is going to expend :P:P
01/10/2012 20:41 DarkTwilight#10
Is there any ENGLISH tutorial on how to do hooking and so on? cause my german is not good enough to understand the link you gave me :) its awesome but, my german is not good enough lol =)
01/10/2012 20:52 MoepMeep#11
gamedeception ;o
01/13/2012 22:47 IceTray#12
From my WoW Anti AFK. Works if it's in the background, too.

Code:
HWND hwnd = FindWindow(NULL, "World of Warcraft");

SendMessage(hwnd, WM_KEYDOWN, VK_LSHIFT, NULL); // push key
Sleep(10); // hold key. not always works without this.
SendMessage(hwnd, WM_KEYUP, VK_LSHIFT, NULL); // release key
VK_LSHIFT is left shift. For a list look here: [Only registered and activated users can see links. Click Here To Register...]
01/13/2012 23:47 Dr. Coxxy#13
Quote:
Originally Posted by IceTray View Post
From my WoW Anti AFK. Works if it's in the background, too.

Code:
HWND hwnd = FindWindow(NULL, "World of Warcraft");

SendMessage(hwnd, WM_KEYDOWN, VK_LSHIFT, NULL); // push key
Sleep(10); // hold key. not always works without this.
SendMessage(hwnd, WM_KEYUP, VK_LSHIFT, NULL); // release key
VK_LSHIFT is left shift. For a list look here: [Only registered and activated users can see links. Click Here To Register...]
we/he already stated that sendmessage doesnt work 4 his game.

Quote:
Yes I tried Send/Post message but those do nothing for me.
Nothing happens at all.
01/14/2012 01:24 IceTray#14
Quote:
Originally Posted by Dr. Coxxy View Post
we/he already stated that sendmessage doesnt work 4 his game.
Maybe because there is no break between push key and release key. That most didn't work when I try it. But with a little break it works fine.