I am trying to send some keystrokes to an already running application.
Everything works fine if I use any other function then PostMessage.
When I use PostMessage, nothing happens!!!
But I have been told that I need PostMessage if I want to be able to
send keystrokes even when (for w/e reason) the window I am sending it to is
not focussed.
I know it is not 100% safe but, it works good enough for what I want.
I hope someone can help me out!
// bring the window to the front
HWND GameWindow = FindWindow(0, L"Naamloos - Kladblok");
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);
}
}
According to google, "Naamloos - Kladblok" translates to "Untitled - Notepad", if you are sending key strokes to Notepad, you will need a handle to the edit control. You may also need a slight delay between the keydown and keyup messages.
By the looks of it though, you wish to send key strokes to a game window? it's not uncommon for PostMessage to be blocked via anti cheat software.
I'm getting sick of all these game hacking threads.
it has nothing to do with game hacking, the whole point is that I am learning C++ and this part I found interesting. I know it is possible since people have tools that do what I am wanting to do here, but I have no clue how they did it.
This is just a learning process for me, no more, no less.
And if you are sick of posts, then why bother reading this forum? might be best for you to move on and go somewhere else or dont visit forums at all anymore.
Besides that, the code works. If I change PostMessage for any other functions (sendkeys for example) then it works perfectly. It is just not working with PostMessage ;)
Besides that, the code works. If I change PostMessage for any other functions (sendkeys for example) then it works perfectly. It is just not working with PostMessage ;)
PostMessage does not function the same as SendKeys (.net ?) or "any" other function, see the first paragraph of my post above for a possible solution.
I have tried various functions now, the only one which seems to work is keybd_event(VkKeyScan('H'),0,0,0);
Any other function like sendkey, PostMessage or anything else is simple not doing anything at all. No matter if I send it to Notepad, Paint or any other program.
The header <windows.h> is the right one though?
Anyways adding a pause didnt help, changing the name from Dutch to English didnt help either, the program will now freeze (ofcourse, since I have no check for window found or not).
See MSDN for the LPARAM details of WM_KEYDOWN/WM_KEYUP:-
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646281
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280
Also, play around with Spy++:-
http://mdb-blog.blogspot.com/2010/11/microsoft-spy-or-spyxx-for-download.html
Awesome, a lot of stuff that I dont know yet but thats the whole point lol, learn learn and learn more :)
Thanks a lot for your answer sloppy, I will give this a try after I get home from work!
No clue what lparam does (my english is to bad to fully understand it) but I just filled in a number (29 lol) and its working now, thanks a lot sloppy, now this is working it means tearing it to pieces and start learing this lol. Thanks again you are awesome! :D