0

I'm writing a bot that should run on an executable in the background.

SendMessage(hWnd, WM_KEYDOWN, ((IntPtr)k), (IntPtr)0);

The line above will send a key to the process. If my cursor is in the game's chat box, it will type in the input into the chatbox.

If I am not in the chatbox and send that, it will do nothing until I tab into the process in which it acts as if I am holding that key down (I will walk in the direction of the key I am holding) until I press the key I sent in ('s' is the test key I am using) where it will stop. I also tried sending a message right after using the flag WM_KEYUP, but that didn't stop anything.

SetForegroundWindow (hWnd);
SendMessage(hWnd, WM_KEYDOWN, ((IntPtr)k), (IntPtr)0);

This segment will target the process and start moving.

I've been all over the internet trying to figure out how to make this work while in the background, as well as only send 1 input. I've messed around with spy++ but I figured that if it works when I send the command when I am tabbed in, it must be something else.

Here is the full class that I am operating with http://pastebin.com/Jbx9BfSm

user1801067
  • 133
  • 2
  • 12

1 Answers1

0

You need to set the focused window after the popup form is brought to the front. The focused window can be a button, listbox etc

Focus and window activation in Win32

enter image description here

Eminem
  • 7,206
  • 15
  • 53
  • 95
  • Ill read through your link, but from what you said it sounds really confusing, lol. Btw, i'm using Unity3D to create the UI, so it might be just a tad different. – user1801067 Mar 22 '16 at 05:22
  • After you bring your form to front, handle the 'Activated' event. In this event set the focus on your chatbox editbox. i.e. the control that needs to receive the input. Then send the message. You may want to add a delay between setting the ForeGroundWindow and SendMessage functions as timing may be of the issue – Eminem Mar 22 '16 at 05:27
  • So that link was discussing how each thread has another thread that goes with it, one is for the window and the other is for the thread that receives user input. When using spy++ my window has no children. I'll try setting up a delay, but would you mind elaborating on what you mean when you say, "handle the 'Activated' event"? – user1801067 Mar 22 '16 at 05:34
  • Activated event. https://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated(v=vs.110).aspx My understanding of your problem is that you are able to activate the form but the keystrokes doesnt reach its intended destination if your form was initially in the background. However if your form is initially in the foreground and focus is on your chatbox edit control then the messages are received. So I'm simply thinking that when you handle your form_activated event, set the focus on your chatbox editbox THEN send the message – Eminem Mar 22 '16 at 05:39
  • Oh, let me rephrase a little bit. When I send the message, while the games client has the cursor in the chat box, it displays the char I sent in the chat box (This is while the process is in the background). When the cursor is not in the chatbox, the game receives the message but doesnt do anything with it until i make that game the foreground target, in which case it will react to my keypress. I dont care about the chat, I just put it in there because its something interesting that I noticed. – user1801067 Mar 22 '16 at 05:45
  • Instead of using FindWindow use FindWindowEx. It allows you to find the actual control that needs to receive the input. Search for 'lpszClass' on the following page and check out the accepted answer http://stackoverflow.com/questions/4683649/send-keystrokes-to-a-program-even-if-its-in-background-using-c-sharp – Eminem Mar 22 '16 at 06:05
  • When I drag the crosshairs like it says, it doesn't show any child. Its just one window. For the FindWindowEx, it has a few parameters that I dont think exist, such as hwndChildAfter – user1801067 Mar 22 '16 at 06:32