Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] SendMessage / PostMessage Keys to non-focus window?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
phillydub
Newbie cheater
Reputation: 0

Joined: 11 Aug 2007
Posts: 10

PostPosted: Tue Sep 29, 2009 6:06 pm    Post subject: [C#] SendMessage / PostMessage Keys to non-focus window? Reply with quote

Ok, so I am writing a bot and I've been using keybd_event to send keys to the game. I want to be able to send these keys to the game when the game is NOT the focused window, so keybd_event and SendInput do not work.

I have read around, and apparently the way to do this is to export the SendMessage and PostMessage functions from the WinAPI, and send WM_KEYDOWN, WM_KEYUP messages to the game. However, I am having some problems with getting them to work correctly.

For example, when I use PostMessage in the following manner:
Code:

PostMessage(hWnd_Game, WM_KEYDOWN, (int)Keys.B, 0);
PostMessage(hWnd_Game, WM_KEYUP, (int)Keys.B, 0)


It sends the 'B' key twice for some reason, I believe it has to do with the lParam I am giving to PostMessage. Here is some information regarding the WM_KEYDOWN message from MSDN:

Code:

lParam
    Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.

    0-15
        Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
    16-23
        Specifies the scan code. The value depends on the OEM.
    24
        Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
    25-28
        Reserved; do not use.
    29
        Specifies the context code. The value is always 0 for a WM_KEYDOWN message. The value is always 0 for a WM_KEYUP message.
    30
        Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up. The value is always 1 for a WM_KEYUP message.
    31
        Specifies the transition state. The value is always zero for a WM_KEYDOWN message. The value is always 1 for a WM_KEYUP message.


If this is the issue, could anyone explain what I need to use for lParam in order to send the keypress correctly?

Also, I have noticed that some keys, such as the spacebar, are not being recieved by the game SOMETIMES. For example, pressing spacebar while playing the game makes your character jump. When I send the Spacebar using PostMessage, it doesn't make my character jump. HOWEVER, if I have the in-game chat box focused, it DOES print a space in my chat text. Why would PostMessage be able to send spacebar to the chat text, but not to the game otherwise?

If anyone has any insight on using the PostMessage / SendMessage API functions (or any other method) to RELIABLY SEND KEYS to a non-focused window, any help would be greatly appreciated.

Here's some example C# source code if anyone wants to play around with it:
www_filedropper_com/backgroundtest

(sorry apparently i cant post real URLs yet..)
Back to top
View user's profile Send private message
Sneak
Grandmaster Cheater Supreme
Reputation: 0

Joined: 05 Nov 2008
Posts: 1895

PostPosted: Tue Sep 29, 2009 7:31 pm    Post subject: Reply with quote

1) Add the postmessage API's to your program
2) Define hwnd as

Code:
IntPtr hWnd = FindWindow(
            null,
            "Combat_Arms");



or w/e your window's name is. Then define keydown.

Code:
const int WM_KEYDOWN = 0x100;


then this is how I use postmessage

Code:
PostMessage(hWnd, WM_KEYDOWN, 0x5a, 0x2c0001);


I forgot the lparam :0x5a, and also the wParam: 0x2c0001.

However here are the list of wParams and lParams

http://pastebin.com/f6c1db818

just switch em out Very Happy

hope i helped

_________________
Back to top
View user's profile Send private message
phillydub
Newbie cheater
Reputation: 0

Joined: 11 Aug 2007
Posts: 10

PostPosted: Wed Sep 30, 2009 4:27 am    Post subject: Reply with quote

THanks for the (HUGE) table of values there, but for some reason I am still having problems.

For example.. I try to send SPACE to my game, it doesn't register the space, which should cause my character to jump. HOWEVER, if I set the focus to the in-game chat textbox, it DOES print a space there.

I've been having a lot of problems getting post message to send WM_KEYDOWN and WM_KEYUP to emulate background keys. I know its possible (mmoglider), but its not easy.

I wish someone knew the ins and outs of doing this properly!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites