Reputation: 485
I have seen like 20 questions already (here @ stackOverflow) But for some reason I don't get most of the answers they approach. Some other say they understand but I don't see any piece of working code in my case.
I've trying to send keys to games (foreground window) kinda like a macro, and I've seen how the lParam in: PostMessage(handle, WM_KEYDOWN, (uint)key, 0); actually makes the difference; according to people in the web (they say, but not show code/example). First time employing win functions so I'm kinda lost on how to make it work with some games; let's say "League of Legends", or "Team Fortress 2."
I get it's an extra param sent as a way to interpret the Message. But How do I make it work with most of the games? Does every game work differently?
Thanks in advance, and sorry for my noobie-ism with win functions.
Sincerely,
Armando Leon
Upvotes: 1
Views: 1164
Reputation: 1
I know it's an old thread but i cant let someone say "You can't fake input with PostMessage()". This thread is displayed on google when we search for "how to change lparam postmessage".
Let's imagine I need to send an Enter Key to another program: I'll use Spy++ and press physically on the key I need to check what's the constant I need and what are the lParam values I need. (for the constand you can check here too http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx)
Check the keydown, not the keyup.
So if I need an Enter Key with cRepeat: 1 Scancode : 29 fExtended : 0 AltDown : 0 fRepeat : 0 fUp: 0 ,
I'll have to change the 32 bits of the lParam, I have to understand the array here http://msdn.microsoft.com/en-us/library/ms646280%28v=vs.85%29.aspx
the bits are like that : 31....3 2 1 0
I need 1 for cRepeat so : 0000 0000 0000 0001 (bits 0 to 15 like in the array)
I need 29 (in hexa) for Scancode so : 0010 1001 (bits 16 to 23)
the rest is at zero
Now i just regroup the lparam (in binary) I need : 0010 1001 0000 0000 0000 0001
And I convert it to hexadecimal : 290001
So now in my program it's going to work if I use : PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0x290001);
I hope it'll help people who come here like I did :)
Seb,
Upvotes: 0
Reputation: 612844
You can't fake input with PostMessage()
. Instead you need to use SendInput()
.
Upvotes: 1
Reputation: 21
For key down, you can see what lParam contains at this link: http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx. If you simply pass wParam and 0 for lParam, it may work for games that only use virtual key code (contained in wParam) but not in games that use, for instance, scan code (passed in lParam).
What you can do is write a utility program that prints out the wParam and lParam for key down (and any other events (wm_char, wm_keyup, etc.) you wish to send to the game). Then you can use it to get wParam and lParam for any key combination you want and then if you pass it to any game it should work. Passing all correct values is only way to make sure it would work in all cases.
Upvotes: 1